mercredi 25 août 2021

How to use use numpy random choice to get progressively longer sequences with the same numbers?

What I tried was this:

import numpy as np
def test_random(nr_selections, n, prob):
    selected = np.random.choice(n, size=nr_selections, replace= False, p = prob)
    print(str(nr_selections) + ': ' + str(selected))

n = 100
prob = np.random.choice(100, n)
prob = prob / np.sum(prob) #only for demonstration purpose
for i in np.arange(10, 100, 10):
    np.random.seed(123)        
    test_random(i, n, prob)

The result was:

10: [68 32 25 54 72 45 96 67 49 40]
20: [68 32 25 54 72 45 96 67 49 40 36 74 46  7 21 20 53 65 89 77]
30: [68 32 25 54 72 45 96 67 49 40 36 74 46  7 21 20 53 62 86 60 35 37  8 48
     52 47 31 92 95 56] 
40: ...

Contrary to my expectation and hope, the 30 numbers selected do not contain all of the 20 numbers. I also tried using numpy.random.default_rng, but only strayed further away from my desired output. I also simplified the original problem somewhat in the above example. Any help would be greatly appreciated. Thank you!

Edit for clarification: I do not want to generate all the sequences in one loop (like in the example above) but rather use the related sequences in different runs of the same program. (Ideally, without storing them somewhere)




Aucun commentaire:

Enregistrer un commentaire