Page 1 of 1

Every time the device restarts, the random number is a fixed value

Posted: Tue Apr 27, 2021 1:12 pm
by lgyer
Hi guys

When I use random api to get a random number at hardware rebooting, the value is a fixed number.
Even if I use the time.ticks_ms() to set the different random seed, the value is still a fixed number.

Code: Select all

import random
impot time
seed = time.ticks_ms() % 7
random.seed(seed)
print("{}".random.randint(1,100))
Can someone tell me why is that?

Re: Every time the device restarts, the random number is a fixed value

Posted: Tue Apr 27, 2021 2:40 pm
by Roberthh
There was a change recently to seed the rng with more random value on boot. Which board and firmware version do you use?

Re: Every time the device restarts, the random number is a fixed value

Posted: Wed Apr 28, 2021 2:02 am
by lgyer
Roberthh wrote:
Tue Apr 27, 2021 2:40 pm
There was a change recently to seed the rng with more random value on boot. Which board and firmware version do you use?
Hi Roberthh

My board is ESP32 and firmware version is v1.12. Could you tell me how to fix this problem? Or has the change been merged into v1.15?

Re: Every time the device restarts, the random number is a fixed value

Posted: Wed Apr 28, 2021 5:42 am
by Roberthh
Just use 1.15. The change was merged in v1.14.

Re: Every time the device restarts, the random number is a fixed value

Posted: Wed Apr 28, 2021 6:22 am
by lgyer
Roberthh wrote:
Wed Apr 28, 2021 5:42 am
Just use 1.15. The change was merged in v1.14.
Hi Roberthh

I has tried to copy the extmod/modurandom.c of v1.15 into my project, but the problem is still existing....
Is there any point I should take care?

By the way, I'm not using ctrl+D to reboot, I just turn on/off the power directly. And it's interesting that even if I execute the command "random.randint(1,12)" twice continuely, the number which I get still is same at every boot time.

Thanks

Re: Every time the device restarts, the random number is a fixed value

Posted: Wed Apr 28, 2021 11:25 am
by Roberthh
It is a definition in mpconfigport.h which has to be changed. The new value is:

#define MICROPY_PY_URANDOM_SEED_INIT_FUNC (esp_random())

Re: Every time the device restarts, the random number is a fixed value

Posted: Wed Apr 28, 2021 12:36 pm
by pythoncoder
In my testing on ESP32 with current firmware, uos.urandom() is not deterministic after a power cycle.

Re: Every time the device restarts, the random number is a fixed value

Posted: Thu Apr 29, 2021 2:43 am
by lgyer
Hi Roberthh

I really appreciate your help.
After I add that macro, it can work.

Thanks

Re: Every time the device restarts, the random number is a fixed value

Posted: Fri Apr 30, 2021 3:59 pm
by scruss
just out of interest, where does the source for the random module live? The only source I can find is micropython-lib/random/random.py, which is incomplete and has no code for initializing/seeding.

Re: Every time the device restarts, the random number is a fixed value

Posted: Fri Apr 30, 2021 4:09 pm
by Roberthh
It is a built-in module. The source code is here: https://github.com/micropython/micropyt ... durandom.c