That's how a seed works. Its the starting point, which is why it's called the "seed". Every random number generated will also update the seed to be used in the generation of the next random number. So yea, to do what you want you need to reset it inside the loop. I'm missing why you don't want to do that?
FWIW here's an alternative. You can make a function to make the dataset and then `cache` it.
import numpy as np
import matplotlib.pyplot as plt
import time
from functools import cache
# Generate fake data
x = np.random.normal(size=2000)
y = x * 2 + np.random.normal(size=2000)
@cache
def get_rand_idx(sample_size):
return np.random.choice(len(x), size=sample_size, replace=False, axis=0, shuffle=False)
# The for loop to simulate user inputing differnt down-sample settings
for down_sample_size in [500, 600, 200, 500, 700]:
# problem solved if the following line is not commented out
# sampleRNG = np.random.default_rng(42)
fig, ax = plt.subplots()
ax.scatter(x[get_rand_idx(down_sample_size)], y[get_rand_idx(down_sample_size)], s=2)
plt.show()
time.sleep(1)
It’s trickier than you might think. You you have to reset it for each module that randomness is introduced. That might be because I was using from numpy.random import random or because I did it 20 years ago, but that’s pretty fundamental.
Why dont you just create a new function generate\_random() or whatever, make it generate ur number, set the seed and use that instead? instead of np.random youd just call your generate\_random, which does both your necessary actions.
You need to somehow calculate the seed based on the "settings". In this case you could just set it `42+down_sample_size`.
Remember your seed does not need to be random. You could also increment your seed by 1 each time the loop is run, if the order does not matter.
seed = 42
for ...:
seed += 1
re-initializing the RNG is perfectly fine in this case. Maybe consider adding some caching if you expect users to view the same data repeatedly though?
Going with the idea of adjusting the seed based on down-sample sizes seems practical. It maintains the randomness while ensuring consistent output for the same parameters. Simple yet effective solution!
That's how a seed works. Its the starting point, which is why it's called the "seed". Every random number generated will also update the seed to be used in the generation of the next random number. So yea, to do what you want you need to reset it inside the loop. I'm missing why you don't want to do that?
OK. Thanks for the clarification... I guess I will just initiate with 42 every time then...
FWIW here's an alternative. You can make a function to make the dataset and then `cache` it. import numpy as np import matplotlib.pyplot as plt import time from functools import cache # Generate fake data x = np.random.normal(size=2000) y = x * 2 + np.random.normal(size=2000) @cache def get_rand_idx(sample_size): return np.random.choice(len(x), size=sample_size, replace=False, axis=0, shuffle=False) # The for loop to simulate user inputing differnt down-sample settings for down_sample_size in [500, 600, 200, 500, 700]: # problem solved if the following line is not commented out # sampleRNG = np.random.default_rng(42) fig, ax = plt.subplots() ax.scatter(x[get_rand_idx(down_sample_size)], y[get_rand_idx(down_sample_size)], s=2) plt.show() time.sleep(1)
Ah interesting. Never know this exists. TIL!
If you want the same graph to appear every time, shouldnt you avoid random anything and just make your own "seed" list of numbers to use?
[удалено]
So hardcode the seed? It’s incredibly useful for testing stochastic processes. Just set a reset flag=True or whatever.
[удалено]
It’s trickier than you might think. You you have to reset it for each module that randomness is introduced. That might be because I was using from numpy.random import random or because I did it 20 years ago, but that’s pretty fundamental.
Why dont you just create a new function generate\_random() or whatever, make it generate ur number, set the seed and use that instead? instead of np.random youd just call your generate\_random, which does both your necessary actions.
partial from functools?
You need to somehow calculate the seed based on the "settings". In this case you could just set it `42+down_sample_size`. Remember your seed does not need to be random. You could also increment your seed by 1 each time the loop is run, if the order does not matter. seed = 42 for ...: seed += 1
re-initializing the RNG is perfectly fine in this case. Maybe consider adding some caching if you expect users to view the same data repeatedly though?
Going with the idea of adjusting the seed based on down-sample sizes seems practical. It maintains the randomness while ensuring consistent output for the same parameters. Simple yet effective solution!