License Public Domain
Lines 43
Keywords
prng (2) random (8)
Implements this Algorithm
Permissions
Owner: Stou S.
Viewable by Everyone
Editable by All Siafoo Users
Hide
Easily highlight source code for your blog with our Syntax Highlighter. Join Siafoo Now or Learn More

An implementation of the Wichmann-Hill PRNG Atom Feed 0

In Brief A simple (unoptimized) implementation of the Wichmann-Hill pseudo-random number generator. The prng requires three seeds.
# 's
 1from __future__ import division # we want floating point division
2
3class WichmannHill(object):
4 '''
5 An implementation of the Wichmann-Hill
6 pseudo-random number generator
7 '''
8
9 mix_0 = [171, 172, 170]
10 mix_1 = [177, 176, 178]
11 mix_2 = [2, 35, 63]
12 divs = [30269, 30307, 30323]
13
14 def __init__(self, seeds):
15 self.reset(seeds)
16
17 def reset(self, seeds=None):
18 ''' Reset the prng '''
19
20 if seeds:
21 self.seeds = list(seeds)
22 self.original_seeds = list(seeds)
23 else:
24 self.seeds = list(self.original_seeds)
25
26 def get_random(self):
27 temp = float(0)
28
29 for i, seed in enumerate(self.seeds):
30 seed = self.mix_0[i] * (seed % self.mix_1[i]) - self.mix_2[i]*(seed / self.mix_1[i])
31
32 if seed < 0:
33 seed += self.divs[i]
34
35 temp += seed / self.divs[i]
36 self.seeds[i] = seed
37
38 return temp - int(temp)
39
40def main():
41
42 wh = WichmannHill([10, 25, 17])
43
44 for i in range(10):
45 print wh.get_random()
46
47if __name__ == "__main__":
48 main()

A simple (unoptimized) implementation of the Wichmann-Hill pseudo-random number generator. The prng requires three seeds.