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

Method getran

Lib/test/test_long.py:123–142  ·  view source on GitHub ↗
(self, ndigits)

Source from the content-addressed store, hash-verified

121 # The sign of the number is also random.
122
123 def getran(self, ndigits):
124 self.assertGreater(ndigits, 0)
125 nbits_hi = ndigits * SHIFT
126 nbits_lo = nbits_hi - SHIFT + 1
127 answer = 0
128 nbits = 0
129 r = int(random.random() * (SHIFT * 2)) | 1 # force 1 bits to start
130 while nbits < nbits_lo:
131 bits = (r >> 1) + 1
132 bits = min(bits, nbits_hi - nbits)
133 self.assertTrue(1 <= bits <= SHIFT)
134 nbits = nbits + bits
135 answer = answer << bits
136 if r & 1:
137 answer = answer | ((1 << bits) - 1)
138 r = int(random.random() * (SHIFT * 2))
139 self.assertTrue(nbits_lo <= nbits <= nbits_hi)
140 if random.random() < 0.5:
141 answer = -answer
142 return answer
143
144 # Get random long consisting of ndigits random digits (relative to base
145 # BASE). The sign bit is also random.

Callers 3

test_divisionMethod · 0.95
test_bitop_identitiesMethod · 0.95
test_formatMethod · 0.95

Calls 3

assertGreaterMethod · 0.80
assertTrueMethod · 0.80
randomMethod · 0.45

Tested by

no test coverage detected