Page 1 of 1
Random number generation?
Posted: Wed Nov 30, 2016 7:47 pm
by harambecute101
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?)
Re: Random number generation?
Posted: Thu Dec 01, 2016 8:03 am
by pythoncoder
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
or
Re: Random number generation?
Posted: Fri Dec 16, 2016 10:19 am
by ovelap
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
Re: Random number generation?
Posted: Sat Dec 17, 2016 8:13 am
by pythoncoder
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
Re: Random number generation?
Posted: Thu Jan 19, 2017 7:18 am
by pythoncoder
Ignore the above, MicroPython provides a better way:
Code: Select all
import urandom
urandom.getrandbits(30)