Skip to content

Commit a3df207

Browse files
committed
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>
1 parent 8631225 commit a3df207

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

python-stdlib/random/random.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
from urandom import *
22

3+
_getrandbits32 = getrandbits
4+
5+
6+
def getrandbits(bits: int) -> int:
7+
n = bits // 32
8+
d = 0
9+
for i in range(n):
10+
d |= _getrandbits32(32) << (i * 32)
11+
12+
r = bits % 32
13+
if r >= 1:
14+
d |= _getrandbits32(r) << (n * 32)
15+
16+
return d
17+
318

419
def randrange(start, stop=None):
520
if stop is None:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy