lundi 19 décembre 2022

random number with specific condition in python

I'd like to generate an int random number between 0 and 1 n number of times. I know the code to generate this:

num_samp = np.random.randint(0, 2, size= 20)

However, I need to specify a condition that say 1 can only appear a number times and the rest should be zeros. For instance if I want 1 to appear only 5 times from the above code, then I would have something like this [0,1,0,0,0,1,1,0,0,0,1,0,1,0,0,0,0,0,0,0]

Can someone help with the code to generate something like this? Thanks




Aucun commentaire:

Enregistrer un commentaire