While I was testing whether Python lists keep iterating if you add new items to the list, I stumbled upon a situation that I found rather remarkable, especially if you do the maths. My code looked like this;
import random
listy = [1]
for _ in listy:
if random.random() > 0.25:
listy.append(100)
print(listy)
print(len(listy))
Usually, the lists ended up not being so big - most were below eight, and one was about 12. When I kept spamming the terminal with the program for about 20-30-ish times, I stumbled upon a list with 35 elements.
[1, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
35
If you do the maths, the chance of getting this, is so astronomically large that I'm almost starting to doubt there's a different reason I got this humongous list; is this an example of when a pseudo-random generator is really pseudo-random? Am I miscalculating something? Did I in fact stumble upon an event that is absurdly rare?
I know this isn't a traditional question, and ironically, the whole point of this question is that one isn't supposed to be able to recreate this event.
Aucun commentaire:
Enregistrer un commentaire