in ESP32 there is a random.randint() function
in ESP866 no.
how to get any randomize value in ESP866 ?
random.randint() not exists in esp8266 ?
-
- Posts: 169
- Joined: Fri Aug 19, 2016 11:55 am
Re: random.randint() not exists in esp8266 ?
As far as I recall, random.getrandbits() is the only function available on ESP8266 (check help(random)). You can build anything else you need on top of that. Here’s a ready-to-use example.
Re: random.randint() not exists in esp8266 ?
thanks Christian
got it
got it
Re: random.randint() not exists in esp8266 ?
I found the below from this topic: viewtopic.php?t=6158
Code: Select all
import urandom
def randint(min, max):
div = 0x3fffffff // span
span = max - min + 1
offset = urandom.getrandbits(30) // div
val = min + offset
return val
-
- Posts: 169
- Joined: Fri Aug 19, 2016 11:55 am
Re: random.randint() not exists in esp8266 ?
This doesn’t work for several reasons.
- Lines are in the wrong order (variable is used before assignment).
- It gives you values outside of the requested range (try with min = 0, max = 0x1fffffff, getrandbits(30) = 0x3fffffff).
- It doesn’t give you a uniform distribution, some values are more likely than others.