random module

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
ytsejam
Posts: 2
Joined: Tue Jan 17, 2017 5:16 pm

random module

Post by ytsejam » Wed Jan 18, 2017 6:49 pm

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


ytsejam
Posts: 2
Joined: Tue Jan 17, 2017 5:16 pm

Re: random module

Post by ytsejam » Wed Jan 18, 2017 10:10 pm

Thank you for answer.
I tried to import the pyp form, but apparently is not included in my micropython.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: random module

Post by pythoncoder » Thu Jan 19, 2017 7:11 am

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.

mlupo
Posts: 4
Joined: Fri Nov 11, 2016 2:11 am

Re: random module

Post by mlupo » Mon Jan 15, 2018 4:48 pm

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:

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)
 
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:

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
For my purposes, I needed a way to replicate the random.choice() function, for which this is useful:

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

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: random module

Post by pythoncoder » Tue Jan 16, 2018 11:58 am

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.

mlupo
Posts: 4
Joined: Fri Nov 11, 2016 2:11 am

Re: random module

Post by mlupo » Tue Jan 16, 2018 2:50 pm

Thanks Peter! Yours is a much more elegant solution :)

Post Reply