The whrandom Module
The whrandom
module, shown in Example 2-33, provides a pseudo-random number generator (based on an
algorithm by Wichmann and Hill, 1982). Unless you need several
generators that do not share internal state (for example, in a
multithreaded application), it’s better to use the functions in the
random
module
instead.
Example 2-33. Using the whrandom Module
File: whrandom-example-1.py import whrandom # same as random print whrandom.random() print whrandom.choice([1, 2, 3, 5, 9]) print whrandom.uniform(10, 20) print whrandom.randint(100, 1000)0.113412062346
1
16.8778954689
799
Example 2-34 shows how to create multiple generators by creating instances of the
whrandom
class.
Example 2-34. Using the whrandom Module to Create Multiple Random Generators
File: whrandom-example-2.py import whrandom # initialize all generators with the same seed rand1 = whrandom.whrandom(4,7,11) rand2 = whrandom.whrandom(4,7,11) rand3 = whrandom.whrandom(4,7,11) for i in range(5): print rand1.random(), rand2.random(), rand3.random()0.123993532536 0.123993532536 0.123993532536
0.180951499518 0.180951499518 0.180951499518
0.291924111809 0.291924111809 0.291924111809
0.952048889363 0.952048889363 0.952048889363
0.969794283643 0.969794283643 0.969794283643
Get Python Standard Library now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.