MCPcopy Create free account
hub / github.com/ActiveState/code / randrange

Class randrange

recipes/Python/473808_randomized_integer_range/recipe-473808.py:8–44  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6TEASBOXSHIFT = 7
7
8class randrange(object):
9 def __init__(self, start, stop):
10 self.start = start
11 self.max = stop - start
12 self.sbox = array.array('I', [ random.randint(0, 0xffffffffL)
13 for i in range(TEASBOXSIZE) ])
14 bits = 0
15 while (1 << bits) < self.max:
16 bits += 1
17 self.left = bits / 2
18 self.right = bits - self.left
19 self.mask = (1 << bits) - 1
20
21 if TEASBOXSIZE < (1 << self.left):
22 self.sboxmask = TEASBOXSIZE - 1
23 self.kshift = TEASBOXSHIFT
24 else:
25 self.sboxmask = (1 << self.left) - 1
26 self.kshift = self.left
27
28 def __iter__(self):
29 enc = 0
30 for i in range(self.max):
31 c = self.max
32 while c >= self.max:
33 c = enc
34 enc += 1
35 s = 0
36 for j in range(TEAROUNDS):
37 s += TEADELTA
38 c ^= (self.sbox[(c ^ s) & self.sboxmask] << self.kshift)
39 c = (c + s) & self.mask
40 c = ((c << self.left) | (c >> self.right)) & self.mask
41 yield self.start + c
42
43 def __len__(self):
44 return self.max

Callers 15

sortTaskFunction · 0.90
is_primeFunction · 0.90
randprimeFunction · 0.90
keygenFunction · 0.90
recipe-140328.pyFile · 0.90
recipe-577187.pyFile · 0.90
recipe-576930.pyFile · 0.90
recipe-577073.pyFile · 0.90
numberFunction · 0.90
recipe-577059.pyFile · 0.90
is_primeFunction · 0.90
randprimeFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected