Random number generation?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
harambecute101
Posts: 12
Joined: Mon Nov 28, 2016 10:04 pm

Random number generation?

Post by harambecute101 » Wed Nov 30, 2016 7:47 pm

Hi,
For those of you who has seen the "Micropython Overview" video,
https://www.youtube.com/watch?v=5LbgyDmRu9s
You can see that the pyboard demonstrates its random number generation capabilities with the 4 LEDs dice program.
I am a beginner and I am currently working on the program.
I have managed to make the leds scroll but I want to make the board choose a random LED.
I have the pyblite 1.0 board, which doesn't have the random number generator.
Is there still a way to generate random numbers on the board? I saw that the "random" module is not on the board. I would like to know how to import the module into the pyboard.(Does pyboard accept normal python modules? Like, ones that are not micropython modules? How can I add them to the pboard?)

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

Re: Random number generation?

Post by pythoncoder » Thu Dec 01, 2016 8:03 am

See pyb.rng(): http://docs.micropython.org/en/latest/p ... -functions. In general MicroPython has its own libraries designed to run on microcontrollers. The native Python libraries are often too large for this purpose.

Note that pyb.rng() returns a 30 bit number. If you want (say) a number n where 0 <= n <= 1 you'll need to write

Code: Select all

n = pyb.rng() / (2 ** 30 - 1)
or

Code: Select all

n = pyb.rng() / 0x3fffffff
Peter Hinch
Index to my micropython libraries.

ovelap
Posts: 4
Joined: Thu Dec 15, 2016 12:22 pm

Re: Random number generation?

Post by ovelap » Fri Dec 16, 2016 10:19 am

Hi,
I want to achive similar result - blinking with random LED. But it not works with pyboard lite.
I tried import pyb. But help(pyb) do not list pyb.rng() method. Another solution?

Thanks

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

Re: Random number generation?

Post by pythoncoder » Sat Dec 17, 2016 8:13 am

One approach is to use a pseudo random binary sequence generator. I have some rather ancient code here:

Code: Select all

class Rando(object):
    def __init__(self, strInit = ""):
        self.x  = [1200, 2345, 5798]            # Arbitrary initial state
        self.modify(strInit)                    # Modify initial values using the passed string
        self.xSave = [self.x[0], self.x[1], self.x[2]] # Save a copy of the initial values

    def reset(self):                            # Restore from saved value
        self.x = [self.xSave[0], self.xSave[1], self.xSave[2]]

    def modify(self, strMod):                   # Modify initial generator state using a string
        for strX in strMod:
            nIdx = self.value(3)                # Iterate the generator and get a random index between 0 and 2
            self.x[nIdx] ^= ord(strX)           # Modify the integer stored at that index using the passed character
            if self.x[nIdx] == 0:               # Disallow zero values
                self.x[nIdx] = ord(strX)
            
    def value(self, nModulo):                   # Return a random integer in range 0 to nModulo -1
        self.x[0] = 171*(self.x[0] % 177) - 2*(self.x[0] //177)
        if self.x[0] < 0:
            self.x[0] += 30269
        self.x[1] = 172*(self.x[1] % 176) - 35*(self.x[1] // 176)
        if self.x[1] < 0:
            self.x[1] += 30307
        self.x[2] = 170*(self.x[2] % 178) - 63*(self.x[2] // 178)
        if self.x[2] < 0:
            self.x[2] += 30323
        temp = self.x[0]/30269.0 + self.x[1]/30307.0 + self.x[2]/30323.0
        temp = (temp - int(temp))*nModulo
        return int(temp)
If you want random numbers between 0 and 99 use as follows:

Code: Select all

a = Rando('rats') # Arbitrary string scrambles initial value
for _ in range(50):
	print(a.value(100)) # Integers modulo 100
Peter Hinch
Index to my micropython libraries.

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

Re: Random number generation?

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

Ignore the above, MicroPython provides a better way:

Code: Select all

import urandom
urandom.getrandbits(30)
Peter Hinch
Index to my micropython libraries.

Post Reply