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 timeimport numpy.random
orfrom numpy.random import something
in code), constructs globalrandomstate
, default arguments (meaningseed=none
). randomstate
's initializer passesseed
argument onseed
method.randomstate.seed
, when callednone
, uses appropriate source of system entropy platform (like/dev/urandom
).- when call top-level
rand
, uses globalrandomstate
.
* 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
Post a Comment