Hello everyone, I need to use the random module for integers with micropython, please give me some help? the random module does not exist for the micropython
Thanks
random module
Re: random module
A quick search of the forum yields the following threads related to random:
http://forum.micropython.org/viewtopic. ... 27&p=16541
http://forum.micropython.org/viewtopic. ... 530#p16530
http://forum.micropython.org/viewtopic. ... 609#p16609
http://forum.micropython.org/viewtopic. ... 27&p=16541
http://forum.micropython.org/viewtopic. ... 530#p16530
http://forum.micropython.org/viewtopic. ... 609#p16609
Re: random module
Thank you for answer.
I tried to import the pyp form, but apparently is not included in my micropython.
I tried to import the pyp form, but apparently is not included in my micropython.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: random module
The last of the links Dave provided is worth trying:
.
Code: Select all
import urandom
urandom.getrandbits(30)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: random module
Hello,
I know this is an old post, but I stumbled across it on my way to finding my current solution:
First, this: https://stackoverflow.com/a/1969274 Stack Overflow answer has a nice general purpose function for mapping one range of values onto an other. From that post, the function in the accepted answer looks like this:
So, we need to input a value, we need to know the range of that value, and we need the range we want to map that value onto. Well, the highest unsigned 16bit number is 65535 (right?), which means we can use the translate function above together with the aforementioned urandom.getrandbits(). See below, and substitute your numbers of choice for the minNum and maxNum variables:
For my purposes, I needed a way to replicate the random.choice() function, for which this is useful:
I know this is an old post, but I stumbled across it on my way to finding my current solution:
First, this: https://stackoverflow.com/a/1969274 Stack Overflow answer has a nice general purpose function for mapping one range of values onto an other. From that post, the function in the accepted answer looks like this:
Code: Select all
def translate(value, leftMin, leftMax, rightMin, rightMax):
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (float)
valueScaled = float(value - leftMin) / float(leftSpan)
# Convert the 0-1 range into a value in the right range.
return rightMin + (valueScaled * rightSpan)
Code: Select all
num = translate(urandom.getrandbits(16), 0, 65535, minNum, maxNum)
integer = math.floor(num) # the translate function returns a float, which we gotta deal with somehow
Code: Select all
def pick_item(sequence):
randomBits = urandom.getrandbits(16)
cieling = len(sequence)
num = translate(randomBits, 0, 65535, 0, cieling)
choice = sequence[math.floor(num)]
return choice
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: random module
The following avoids the use of floats and should work for any sequence length which is plausible on the ESP8266.
Code: Select all
def pick_item(sequence):
div = 0x3fffffff // len(sequence)
return sequence[urandom.getrandbits(30) // div]
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: random module
Thanks Peter! Yours is a much more elegant solution