python - NumPy random seed produces different random numbers -


i run following code:

 np.random.randomstate(3)  idx1 = np.random.choice(range(20),(5,))  idx2 = np.random.choice(range(20),(5,))    np.random.randomstate(3)  idx1s = np.random.choice(range(20),(5,))  idx2s = np.random.choice(range(20),(5,))        

the output following:

idx1:  array([ 2, 19, 19,  9,  4])   idx1s: array([ 2, 19, 19,  9,  4])    idx2:  array([ 9,  2,  7, 10,  6])  idx2s: array([ 5, 16,  9, 11, 15])  

idx1 , idx1s match, idx2 , idx2s not match. expect once seed random number generator , repeat same sequence of commands - should produce same sequence of random numbers. not true? or there else missing?

you're confusing randomstate seed. first line constructs object can use random source. example, make

>>> rnd = np.random.randomstate(3) >>> rnd <mtrand.randomstate object @ 0xb17e18cc> 

and then

>>> rnd.choice(range(20), (5,)) array([10,  3,  8,  0, 19]) >>> rnd.choice(range(20), (5,)) array([10, 11,  9, 10,  6]) >>> rnd = np.random.randomstate(3) >>> rnd.choice(range(20), (5,)) array([10,  3,  8,  0, 19]) >>> rnd.choice(range(20), (5,)) array([10, 11,  9, 10,  6]) 

[i don't understand why idx1 , idx1s agree-- didn't post self-contained transcript, suspect user error.]

if want affect global state, use seed:

>>> np.random.seed(3) >>> np.random.choice(range(20),(5,)) array([10,  3,  8,  0, 19]) >>> np.random.choice(range(20),(5,)) array([10, 11,  9, 10,  6]) >>> np.random.seed(3) >>> np.random.choice(range(20),(5,)) array([10,  3,  8,  0, 19]) >>> np.random.choice(range(20),(5,)) array([10, 11,  9, 10,  6]) 

using specific randomstate object may seem less convenient @ first, makes lot of things easier when want different entropy streams can tune.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -