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

Method _randbelow_with_getrandbits

Lib/random.py:245–252  ·  view source on GitHub ↗

Return a random int in the range [0,n). Defined for n > 0.

(self, n)

Source from the content-addressed store, hash-verified

243 break
244
245 def _randbelow_with_getrandbits(self, n):
246 "Return a random int in the range [0,n). Defined for n > 0."
247
248 k = n.bit_length()
249 r = self.getrandbits(k) # 0 <= r < 2**k
250 while r >= n:
251 r = self.getrandbits(k)
252 return r
253
254 def _randbelow_without_getrandbits(self, n, maxsize=1<<BPF):
255 """Return a random int in the range [0,n). Defined for n > 0.

Callers

nothing calls this directly

Calls 2

bit_lengthMethod · 0.80
getrandbitsMethod · 0.45

Tested by

no test coverage detected