Page 1 of 1

urandom.randint() replacement?

Posted: Sun Mar 24, 2019 2:52 pm
by hdsjulian
I want to generate a random number between 0 and 360. However, urandom only has getrandbits() implemented. Any other clean way in Micropython to do what i'm trying to achieve?

Re: urandom.randint() replacement?

Posted: Sun Mar 24, 2019 3:41 pm
by bitninja
Not sure where I found it... :?

Code: Select all

import urandom

def randint(min, max):
    span = max - min + 1
    div = 0x3fffffff // span
    offset = urandom.getrandbits(30) // div
    val = min + offset
    return val