kopia lustrzana https://github.com/micropython/micropython-lib
python-stdlib/random: Add getrandbits with no limit on number of bits.
Thanks to Macarthur Inbody aka @133794m3r for the implementation. Signed-off-by: Damien George <damien@micropython.org>pull/427/head
rodzic
8631225b7f
commit
a3df207934
|
@ -1,5 +1,20 @@
|
|||
from urandom import *
|
||||
|
||||
_getrandbits32 = getrandbits
|
||||
|
||||
|
||||
def getrandbits(bits: int) -> int:
|
||||
n = bits // 32
|
||||
d = 0
|
||||
for i in range(n):
|
||||
d |= _getrandbits32(32) << (i * 32)
|
||||
|
||||
r = bits % 32
|
||||
if r >= 1:
|
||||
d |= _getrandbits32(r) << (n * 32)
|
||||
|
||||
return d
|
||||
|
||||
|
||||
def randrange(start, stop=None):
|
||||
if stop is None:
|
||||
|
|
Ładowanie…
Reference in New Issue