Home > database >  Python Nump: Random Seed
Python Nump: Random Seed

Time:11-03

num= 100
obs=[]
np.random.seed(1)


for i in range(num):
  
  N=[]
  CF3_1=[]
  CF3_2=[]
  i=2

  salvage= np.random.triangular(salvage_min, salvage_mode, salvage_max)
  inv= np.random.uniform(inv_min, inv_max)


  for y in range(0,5):
    
    gt= 14.747101   11.664259 * np.random.beta(0.645868, 0.920589)
    print(gt)
    

I am trying to get gt to start with np.random.seed(1) but it seems like gt does not start with seed 1.

np.random.seed(1)
gt= 14.747101   11.664259 * np.random.beta(0.645868, 0.920589)
print(gt)

produces a result of:

17.889105013452703
14.747134750650536
19.474766202077234
16.967191836499126
18.465043981244925
18.03420202040634
15.794033025059752

while the result of my code is:

14.747134750650536
19.474766202077234
16.967191836499126
18.465043981244925
18.03420202040634

Notice that the result of my code is the exact same, only that it skipped 17.889105013452703.

CodePudding user response:

This is to be expected. np.random.triangular and np.random.uniform each consume a random double (64 bits), while np.random.beta consumes two doubles; see source.

So after you reset the seed, calling np.random.triangular and np.random.uniform once each will consume two doubles, which means the values returned by successive np.random.beta calls will be one value behind what it returns without those two prior calls.

If you want your gt sequence to start with np.random.seed(1) then you should call np.random.seed(1) after you've called the other functions that consume random values (ie, before the inner for loop).

If you want np.random.triangular and np.random.uniform to generate random values (ie, not be repeatable due to np.random.seed(1)) you should create a separate random generator to use with them.

  • Related