lundi 5 avril 2021

How to generate a 0-1 sequence according to different probabilities efficiently?

I want to get a random 0-1 sequence. Now I generate the number one by one. My code is as follows:

p_arr = [0.1, 0.5, 0.3, 0.8]
seq = []
for pb in p_arr:
  seq.append(np.random.choice(2, 1, p=[1-pb, pb]))

It's very time-consuming when the length of p_arr is very large (i.e. 10000). I wonder if there is another faster way to do this.




Aucun commentaire:

Enregistrer un commentaire