Logo Search packages:      
Sourcecode: vegastrike-data version File versions  Download package

random Namespace Reference

Detailed Description

Random variable generators.

       uniform within range

       pick random element
       generate random permutation

distributions on the real line:
       normal (Gaussian)
       negative exponential

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()
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()
>>> g.jumpahead(6953607871644L - 1) # move *back* one
>>> g.random()


class  Random


def __getstate__
 ---- Methods below this point do not need to be overridden when ---- subclassing for the purpose of using a different core generator.
def __setstate__
def __whseed
def _test
def _test_generator
 -------------------- test program --------------------
def _verify
def betavariate
 -------------------- beta -------------------- See http://sourceforge.net/bugs/?func=detailbug&bug_id=130030&group_id=5470 for Ivan Frohne's insightful analysis of why the original implementation:
def choice
 -------------------- sequence methods -------------------
def cunifvariate
 -------------------- circular uniform --------------------
def expovariate
 -------------------- exponential distribution --------------------
def gammavariate
 -------------------- gamma distribution --------------------
def gauss
 -------------------- Gauss (faster alternative) --------------------
def getstate
def jumpahead
def lognormvariate
 -------------------- lognormal distribution --------------------
def normalvariate
 -------------------- normal distribution --------------------
def paretovariate
 -------------------- Pareto --------------------
def randint
def random
def randrange
 -------------------- integer methods -------------------
def seed
 -------------------- core generator -------------------
def setstate
def shuffle
def stdgamma
def uniform
 -------------------- real-valued distributions -------------------
def vonmisesvariate
 -------------------- von Mises distribution --------------------
def weibullvariate
 -------------------- Weibull --------------------
def whseed


list __all__
float _e = 2.7182818284590451
tuple _inst = Random()
float _pi = 3.1415926536
 betavariate = _inst.betavariate
 choice = _inst.choice
 cunifvariate = _inst.cunifvariate
 expovariate = _inst.expovariate
 gammavariate = _inst.gammavariate
 gauss = _inst.gauss
 getstate = _inst.getstate
 jumpahead = _inst.jumpahead
tuple LOG4 = _log(4.0)
 lognormvariate = _inst.lognormvariate
 normalvariate = _inst.normalvariate
 paretovariate = _inst.paretovariate
 randint = _inst.randint
 random = _inst.random
 randrange = _inst.randrange
 seed = _inst.seed
 setstate = _inst.setstate
float SG_MAGICCONST = 1.0
 shuffle = _inst.shuffle
 stdgamma = _inst.stdgamma
float TWOPI = 2.0
 uniform = _inst.uniform
 vonmisesvariate = _inst.vonmisesvariate
 weibullvariate = _inst.weibullvariate
 whseed = _inst.whseed

Generated by  Doxygen 1.6.0   Back to index