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
Damien George 2021-05-30 16:04:17 +10:00
rodzic 8631225b7f
commit a3df207934
1 zmienionych plików z 15 dodań i 0 usunięć

Wyświetl plik

@ -1,5 +1,20 @@
from urandom import * 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): def randrange(start, stop=None):
if stop is None: if stop is None: