jeudi 28 septembre 2023

numpy random number generator: I'm using the same seed, but I'm not getting the same sequence of numbers?

I am generating multiple grids with simulated data (elevation), and as part of that, I add or subtract elevation (according to a function of distance from a center point) from select points. I want the sequence of points where I modify the data (center points), to be the same for each grid, but it doesn't matter where they are so I'm just choosing random points from a uniform distribution. HOWEVER, even with a set seed, I keep getting different sequences of the center points? It's gotta be something simple I'm just missing, but I can't see it. So please help?

Example of paired-down code:

import numpy as np

seed = 3;
rn_gen = np.random.default_rng(seed=seed);
size = 10

for grid in [1, 2]:
    center_x = rn_gen.integers(1, high=100, size = size, endpoint = True);
    center_y = rn_gen.integers(1, high=100, size = size, endpoint = True);
    print("x:", center_x, "y:", center_y);

I consistently get the same answer:

x: [82 9 18 24 19 81 87 59 4 10] y: [34 44 63 48 27 16 70 74 4 12]

x: [46 40 89 52 43 44 67 59 18 74] y: [76 96 79 29 32 65 66 70 87 30]

but why aren't the second x and y the same as the first x and y?

[P.S. In reality, this is written within a function which works out the "area" (arrays of grid node IDs), which is passed to a different function that deals with modifying the data in that area), e.g. given below, in case the function is inferring with my problem].

import numpy as np

seed = 3;
rn_gen = np.random.default_rng(seed=seed);
size = 10

def function1(size):
    center_x = rn_gen.integers(1, high=100, size = size, endpoint = True);
    center_y = rn_gen.integers(1, high=100, size = size, endpoint = True);
    
    return (center_x, center_y)
    

for grid in [1, 2]:
    xs_ys = function1(size); 
    print(xs_ys);

output =
(array([82, 9, 18, 24, 19, 81, 87, 59, 4, 10], dtype=int64), array([34, 44, 63, 48, 27, 16, 70, 74, 4, 12], dtype=int64))(array([46, 40, 89, 52, 43, 44, 67, 59, 18, 74], dtype=int64), array([76, 96, 79, 29, 32, 65, 66, 70, 87, 30], dtype=int64))

Thanks!

I am trying to repeatedly generate the same sequence of random numbers using numpy's random number generator (uniform), but it doesn't generate the same sequence of random numbers for some reason, despite the fact I have set a consistent seed, and I'm not sure where I'm going wrong?




Aucun commentaire:

Enregistrer un commentaire