I'm currently trying to randomize a list of 0s and 1s which should give a random order of zeros and ones with the following constraints: 1) 1/3 of the items have to be 1s (respectively 2/3 are 0s) 2) no more than two 1s should occur consecutively 3) no more than four zeros should occur consecutively
I have worked on an option, but it did not exactly turn out to be what I need. Here's my option:
for prevItem, nextItem in enumerate(WordV[:-1]):
if nextItem == WordV[prevItem+1] and WordV[prevItem+1] == WordV[prevItem+2] and nextItem ==1:
WordV[prevItem+2] = 0
if nextItem == WordV[prevItem+1] and WordV[prevItem+1] == WordV[prevItem+2] and WordV[prevItem+2] == WordV[prevItem+3] and WordV[prevItem+3] == WordV[prevItem+4] and nextItem == 0:
WordV[prevItem+2] = 1
#check the number of ones & zeros
print(WordV)
ones= WordV.count(1)
zeros= WordV.count(0)
print(ones, zeros)
Currently, the number of ones and zeros does not add up to a proportion of 1/3 to 2/3 because the constraints replace numbers. The WordV list is a list containing 24 ones and 48 zeros that is shuffled randomly (with random.shuffle(WordV)).
I'm pretty sure there is a smarter (and more correct) way to integrate the constraints into the code. Any help would be tremendously appreciated!
Aucun commentaire:
Enregistrer un commentaire