MCPcopy Index your code
hub / github.com/python/cpython / getrandbits

Method getrandbits

Lib/random.py:902–908  ·  view source on GitHub ↗

getrandbits(k) -> x. Generates an int with k random bits.

(self, k)

Source from the content-addressed store, hash-verified

900 return (int.from_bytes(_urandom(7)) >> 3) * RECIP_BPF
901
902 def getrandbits(self, k):
903 """getrandbits(k) -> x. Generates an int with k random bits."""
904 if k < 0:
905 raise ValueError('number of bits must be non-negative')
906 numbytes = (k + 7) // 8 # bits / 8 and rounded up
907 x = int.from_bytes(_urandom(numbytes))
908 return x >> (numbytes * 8 - k) # trim excess bits
909
910 def randbytes(self, n):
911 """Generate n random bytes."""

Callers 5

randbytesMethod · 0.45
uuid1Function · 0.45
uuid6Function · 0.45
uuid8Function · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected