Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between np.random.seed() and np.random.RandomState()

I know that to seed the randomness of numpy.random, and be able to reproduce it, I should us:

import numpy as np np.random.seed(1234) 

but what does np.random.RandomState() do?

like image 748
eran Avatar asked Apr 10 '14 17:04

eran


People also ask

What is NP random RandomState ()?

RandomState exposes a number of methods for generating random numbers drawn from a variety of probability distributions. In addition to the distribution-specific arguments, each method takes a keyword argument size that defaults to None . If size is None , then a single value is generated and returned.

What is the difference between NP random rand and NP random random?

The only difference is in how the arguments are handled. With numpy. random. rand , the length of each dimension of the output array is a separate argument.

Is NumPy random seed Global?

The Problem With NumPy's Global Random SeedUsing np. random. seed(number) sets what NumPy calls the global random seed, which affects all uses to the np. random.

What is seed in random state?

Seed is a global pseudo-random generator. However, randomstate is a pseudo-random generator isolated from others, which only impact specific variable.


1 Answers

If you want to set the seed that calls to np.random... will use, use np.random.seed:

np.random.seed(1234) np.random.uniform(0, 10, 5) #array([ 1.9151945 ,  6.22108771,  4.37727739,  7.85358584,  7.79975808]) np.random.rand(2,3) #array([[ 0.27259261,  0.27646426,  0.80187218], #       [ 0.95813935,  0.87593263,  0.35781727]]) 

Use the class to avoid impacting the global numpy state:

r = np.random.RandomState(1234) r.uniform(0, 10, 5) #array([ 1.9151945 ,  6.22108771,  4.37727739,  7.85358584,  7.79975808]) 

And it maintains the state just as before:

r.rand(2,3) #array([[ 0.27259261,  0.27646426,  0.80187218], #       [ 0.95813935,  0.87593263,  0.35781727]]) 

You can see the state of the sort of 'global' class with:

np.random.get_state() 

and of your own class instance with:

r.get_state() 
like image 122
askewchan Avatar answered Sep 21 '22 08:09

askewchan