Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'urandom' module always produces the same sequences #41

Closed
birdistheword96 opened this issue Feb 5, 2021 · 3 comments
Closed

'urandom' module always produces the same sequences #41

birdistheword96 opened this issue Feb 5, 2021 · 3 comments

Comments

@birdistheword96
Copy link

birdistheword96 commented Feb 5, 2021

This is Specific to the Pimoroni Version of RPi Micropython Firmware

I have three RPI Picos and they always produce the same sequence of numbers when I call random.random() with the Pimoroni version of firmware (v0.0.5):

>>> import random
>>> random.random()
0.7111824

>>> random.random()
0.4089468

>>> random.random()
0.09752237

>>> random.random()
0.8465231

The same happens with any of the other 'random' functions, they always produce a replicable sequence.

The standard RPi Micropython port firmware (1.14) from the Official Website does not present this issue, if I install that on all three devices every I always get different numbers.

@sigmaeo
Copy link

sigmaeo commented Feb 6, 2021

What you see is typical for a pseudorandom generator.
I think you simply have to seed it yourself with some random value.
Typically with an noisy ADC converter value or with time evolved since start or last keypress or something, that will be not the same each time.

Maybe something like:
random.seed(machine.ADC(4).read_u16() + machine.ADC(0).read_u16() + utime.ticks_cpu())

This is a seed with temperature, unconnected ADC channel0 (so it is a bit noisy) and a high resolution timer that increments all the time.

@Gadgetoid
Copy link
Member

I've rebased our MicroPython fork on upstream so the next release should - hopefully - fix this.

@Gadgetoid
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants