urandom.randint() replacement?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
hdsjulian
Posts: 31
Joined: Mon Dec 03, 2018 8:29 pm

urandom.randint() replacement?

Post by hdsjulian » Sun Mar 24, 2019 2:52 pm

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?

bitninja
Posts: 165
Joined: Thu Sep 15, 2016 4:09 pm
Location: Spring, Texas

Re: urandom.randint() replacement?

Post by bitninja » Sun Mar 24, 2019 3:41 pm

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

Post Reply