dimanche 6 décembre 2020

incomprehensible random variable output

Here is my code with an explanation to what i'm looking for

#generate a list of variable size between 2 and 6
#each item in the list must contain a float> = 1.5 and <= a 4 and the sum of each item in the list must be >= 8 and <= 12

from random import randint, uniform
import time
NombrePassage = randint(2,6)
passage = []
generated = True
x = 0

while generated:
    heureTotal = 0
    for i in range(NombrePassage):
        tmp = round(uniform(1.5,4),2)
        passage.append(tmp)
        heureTotal = heureTotal + passage[i]
        print(heureTotal)
        time.sleep(0.5) 
    if  heureTotal <= 12.0 and heureTotal >= 8.0:
        print("FINDED")
        generated= False


print(*passage, sep="\n")

But the output is weird. Sometime it works perfectly!

Other times the output is cyclical. For example:

6
3.27
6.6899999999999995
9.33
11.68
14.709999999999999
17.0
3.27
6.6899999999999995
9.33
11.68
14.709999999999999
17.0
3.27
6.6899999999999995
9.33
11.68
14.709999999999999
17.0

The condition is reached but the program does not validate it.

I didn't understand what happened!

(I'm new in dev, and sorry for bad English!)




Aucun commentaire:

Enregistrer un commentaire