mercredi 26 février 2020

Unique variable with multiple set.seed()

I would like to generate a random number 193 times, but set a seed so I can get the same number later on for each instance. Because, I could not find a function to set a seed X times, I wrote the following code:

rand_list_raw = []
val_length = int(len(all_articles)*0.15)
seeds = list(range(0,val_length))
index = 0

while len(rand_list_raw) < val_length:  
        seed = seeds[index]
        random.seed(seed)
        rand_num = random.randrange(0, 1290, 1)
        rand_list_raw.append(rand_num)
        index += 1

Checking the length of the unique variables in rand_list_raw, I concluded that random.seed() has a many-to-one mapping, so that multiple instances of a random seed with a different numbers, may results in the same outcome. It actually makes sense, since the range of input variables in infinite and the range of output variables is finite.

len(set(rand_list_raw))

Is there a way to guarantee different numbers (other than hard coding it)?

Ps. I know that I could also just create a list with unique random numbers within the range and export it. But that's not the point.




Aucun commentaire:

Enregistrer un commentaire