python - Is numpy.random.RandomState() automatically called whenever rand() is called? -


languages c++ require programmer set seed of random number generator, otherwise output same. however, libraries numpy not require initialize seed manually.

for example, code like:

from numpy.random import rand rand() 

gives different result every time.

does mean numpy.random.randomstate(seed=none) called every time call rand?

does mean numpy.random.randomstate(seed=none) called every time call rand?

no, means randomstate seeded once @ startup. if re-seeded every time call rand, there no way explicitly ask repeatable pattern.

the same true python stdlib's random module.

and, despite c++, it's also true c++ stdlib's <random> functions.

all of these document default seed, if don't anything, comes system time or system entropy generator (like /dev/random on *nix systems).

this not case c's rand (which still there in c++, although should treat deprecated*), because c goes out of way require startup must equivalent of calling srand(1).


if you're interested in how "once @ startup" works in numpy:

  • at top level of numpy.random module (which gets run first time import numpy.random or from numpy.random import something in code), constructs global randomstate, default arguments (meaning seed=none).
  • randomstate's initializer passes seed argument on seed method.
  • randomstate.seed, when called none, uses appropriate source of system entropy platform (like /dev/urandom).
  • when call top-level rand, uses global randomstate.

* not because of problem; it's easy enough remember call srand @ start of program. prng explicitly doesn't guarantee cycle length longer 32767, unbiased distribution, etc. bad idea anything…


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -