I am trying to generate a list of 100 elements which consist of the numbers 1 to 4 randomly distributed, but without consecutive repetitions. I do not want to determine whether the numbers 1 to 4 occur the same number of times, I want it to be completely random except for having no consecutive repetitions. I wrote a code that seems to be doing that until it stops and says that the "list index out of range", however I cannot figure out why it is out of range.
from random import randint
guesses = []
for x in range (0, 99):
guess = randint(1,4)
guesses.append(guess)
if x> 0 and guesses[x] == guesses[x-1]:
guesses.remove(guess)
print(guesses)
It should look like this:
123421342312321423124213 (...)23142314213
Aucun commentaire:
Enregistrer un commentaire