jeudi 23 juin 2016

Strange python behaviour random

This small script generates a random float beween 0 and 1 and then if its less than "0.000111111111111" it will print the message "found it"

import random

value = 0.4 / 3600
print value

while True:
    rand = random.random()

    print "RAND IS: " + str(rand) + "----- VAL IS " + str(value)
    if rand < value:
        print "found it"

So seemingly the script runs and it never actually "finds it", however when I comment out the print statement before the if statement the command line is FILLED with "found it" even though the rand value isn't less than value. What the hell?




Aucun commentaire:

Enregistrer un commentaire