| random | index /usr/lib/python2.1/random.py |
Random variable generators.
integers
--------
uniform within range
sequences
---------
pick random element
generate random permutation
distributions on the real line:
------------------------------
uniform
normal (Gaussian)
lognormal
negative exponential
gamma
beta
distributions on the circle (angles 0 to 2pi)
---------------------------------------------
circular uniform
von Mises
Translated from anonymously contributed C/C++ source.
Multi-threading note: the random number generator used here is not thread-
safe; it is possible that two calls return the same random value. However,
you can instantiate a different instance of Random() in each thread to get
generators that don't share state, then use .setstate() and .jumpahead() to
move the generators to disjoint segments of the full period. For example,
def create_generators(num, delta, firstseed=None):
"""Return list of num distinct generators.
Each generator has its own unique segment of delta elements from
Random.random()'s full period.
Seed the first generator with optional arg firstseed (default is
None, to seed from current time).
"""
from random import Random
g = Random(firstseed)
result = [g]
for i in range(num - 1):
laststate = g.getstate()
g = Random()
g.setstate(laststate)
g.jumpahead(delta)
result.append(g)
return result
gens = create_generators(10, 1000000)
That creates 10 distinct generators, which can be passed out to 10 distinct
threads. The generators don't share state so can be called safely in
parallel. So long as no thread calls its g.random() more than a million
times (the second argument to create_generators), the sequences seen by
each thread will not overlap.
The period of the underlying Wichmann-Hill generator is 6,953,607,871,644,
and that limits how far this technique can be pushed.
Just for fun, note that since we know the period, .jumpahead() can also be
used to "move backward in time":
>>> g = Random(42) # arbitrary
>>> g.random()
0.25420336316883324
>>> g.jumpahead(6953607871644L - 1) # move *back* one
>>> g.random()
0.25420336316883324
| Classes | ||||||||||
| ||||||||||
| Functions | ||
| ||
| Data | ||
| LOG4 = 1.3862943611198906 NV_MAGICCONST = 1.7155277699214135 SG_MAGICCONST = 2.5040773967762742 TWOPI = 6.2831853071795862 __all__ = ['Random', 'seed', 'random', 'uniform', 'randint', 'choice', 'randrange', 'shuffle', 'normalvariate', 'lognormvariate', 'cunifvariate', 'expovariate', 'vonmisesvariate', 'gammavariate', 'stdgamma', 'gauss', 'betavariate', 'paretovariate', 'weibullvariate', 'getstate', ...] __file__ = '/usr/lib/python2.1/random.pyc' __name__ = 'random' _e = 2.7182818284590451 _inst = <random.Random instance> _pi = 3.1415926535897931 | ||