I am having a hard time figuring out how the code below works:
import random
for i in range(0, 100):
if not i % 15:
random.seed(1178741599)
print [i+1, "Fizz", "Buzz", "FizzBuzz"][random.randint(0,3)]
I understand that when i
is divisible by 15
, random
will use the same seed
and hence randint(0, 3)
will always return the same index, but when I tried
for i in range(100):
random.seed(1178741599)
print random.randint(0,3) #always print 0
the above code always prints 0
, shouldn't it be (this is the correct behavior, and corresponds to 3
(as the "FizzBuzz"
's index in list [i+1, "Fizz", "Buzz", "FizzBuzz"]
?i+1
in the list.)
And what about when i
is divisible by 3
or 5
?
What is the magic of seed 1178741599
?
Edit: as jonrsharpe pointed out, the fizzbuzz code only works in python 2.x