mercredi 8 juin 2022

How do I randomly fill my list with zeros and ones such that their number is equal?

This is what I am doing right now:

import numpy as np
import matplotlib.pyplot as plt
from math import *

K=np.array([np.random.choice([1,0]) for i in range(20)])
print(K)

The OUTPUT gives:

[1 1 0 1 1 0 0 1 1 0 0 0 1 0 0 1 0 0 0 0]

I want to fill this list like above where the positions for ones and zeros are random but the above method does not render them to be equal in number. I understand why this happens. The "choice" randomly chooses from 1 and 0 so there is no reason for them to be equal in number in the list. But if I want them to be randomly chosen and still be equal in number (10 ones and 10 zeros for the above case) what do I do?




Aucun commentaire:

Enregistrer un commentaire