Instatiable Pseudo-Random Number Generator
vlastimil.brom at gmail.com
Thu Sep 10 11:26:40 CEST 2009
2009/9/10 Hans Georg Schaathun <georg at ii.uib.no>:
> I wonder if someone knows of an API with the features I need...
> random.Random and numpy.random each have only half of it...
> My application includes an object to hold a pseudo-randomly
> generated matrix too large to be kept in memory. Hence I
> try to store only the seed, and generate the numbers on the fly.
> This can be done by giving the object an instance of random.Random.
> As far as I can see, numpy.random has a global state, and thus
> cannot be used by several concurrent random-matrix objects.
> My problem is that random.Random is much slower than numpy.random.
> (Three times slower in a small test.) I believe this is because
> numpy.random can return a row at a time, while random.Random only
> return scalars (as far as I have seen).
> Can anyone recommend a PRNG which supported multiple instances
> with independent states, and that also can return numpy.array (or
> something similar) efficiently?
> The distribution is currently Gaussian.
> Thanks in advance,
> :-- George
I didn't tested the specifications nor the efficiency anywhere near
your requirements, but maybe some suggestions to try anyway.
both have the rand function,
the latter likely faster and more customizable; cf. the help text :
Help on built-in function rand in module gmpy:
rand(opt[,arg]): expose various GMP random-number operations,
depending on value of parameter 'opt' (a string) -- arg is
normally an int or mpz (or else gets coerced to mpz), but
must be a Python mutable sequence when opt is 'shuf':
'init': initialize random-state to support arg bits of 'good
randomness', for arg between 1 and 128 (default 32).
May be called again to change this 'random-quality'.
'qual': returns the number-of-bits-of-good-randomness (0 if
the random-generator not yet initialized), arg ignored.
'seed': set/reset random-state's seed to arg.
'save': get random-state seed (for saving) - arg is ignored.
'next': get random mpz, 0 (included) to arg (excluded)
(default range is 0..2**31).
'floa': get random mpf, range 0<=x<1, with arg meaningful bits
(default, if arg missing or 0, is current 'random quality').
'shuf': random shuffle of Python list (or other mutable
sequence) 'arg'; shuffle is in-place, None returned.
More information about the Python-list