mardi 2 août 2022

Numpy array stuck in while loop if I enter a number greater than 0.1

What I am trying to do is :

Generating 6 random numbers which multipled for a coefficient and then added among themselves give me a value between overall - 0.5 and overall + 0.5. The program works fine with a coefficient in the last position of Gk_coeff (the sixth number of Gk_coeff[5]) which is <= 0.1, but if I enter 0.11, 0.12 (like in the code given) and so on, it stops working. There must be a reason but I really cannot think of it. I've tried using it on linux and windows and the issue persists, so it can't be related to the system.

#!/usr/bin/env python3
import random
import numpy

overall = 83

Gk_coeff = [ 0.23, 0.23, 0.23, 0.23, 0.07, 0.12 ]
Gk_values = numpy.empty(6, dtype=int)

calculated_overall = 0

while not (overall - 0.5 <= calculated_overall <= overall + 0.5) :
    calculated_overall = 0
    for i in range (len(Gk_coeff)):
        Gk_values[i] = random.randint(overall - 7, overall + 7)
        calculated_overall += (Gk_values[i] * Gk_coeff[i])

print(calculated_overall)



Aucun commentaire:

Enregistrer un commentaire