So my main objective in this code is to check if a randomly generated number (s) is equal bigger than any other component of a list of randomly generated numbers. If this is the case, then the number that has been examined should be added to the single random generated number, marked as "used", retired from the list and then the process should start again. In the case that there are two or more numbers that are inferior to the single random generated number, both should be "marked" and retired from the list, but only the biggest of the two numbers should be added to the single generated one for the next loop. At the end it should say how many loops it took to either have all the random numbers in the list "marked" and used, or a loop passes without any number being "marked"
As this explanation might not be clear, I will put an example here. Imagine the single number generated (s) is 0.2, while the list of randomly generated numbers are (0.2, 0.3, 0.4, 0.5). The algorithm would first check s against 0.2, and as it is equal, s would become 0.2 + 0.2 = 0.4, 0.2 would be "marked" and retired from the pool, and a new loop would began. In the next loop, the algorithm would check that both 0.3 and 0.4 are equal or smaller than the new s (0.4), so both would be "marked", retired, and s would become 0.2 + 0.4 = 0.6. In the last cycle, 0.5 would be marked and retired, and s would become 0.2 + 0.5 = 0.7, with 3 loops being required to mark all the numbers.
So far I have come up with this ( disregard the specifications regarding the random number generation, please)
import random
import numpy as np
for x in range(0, 101):
s = random.gauss(0.5, 1)
s = round(s, 2)
if s < 0:
s = 0
if s > 1:
s = 1
print("This is S")
print(s)
ai = []
ai_size = 10
for i in range(ai_size):
num = float(random.gauss(0.5,1))
num = round(num, 2)
if num < 0:
num = 0
if num > 1:
num = 1
ai.append(num)
print("This is the vector of Ai")
print(ai)
ai = sorted(ai)
print("This is the vector numerically ordered")
print(ai)
ai_mean = (np.mean(ai))
ai_var = (np.var(ai))
print(ai_mean)
print(ai_var)
rev = 0
loop = 0
for i in ai:
if s >= i:
s += i
rev += 1
loop += 1
print("This is evolving S")
print(s)
else:
break
print("Loop is over")
print("This is the number of elements within the list that are marked")
print(rev)
print("This is the number of loops")
print(loop)
ai_percentage = rev / ai_size * 100
print("This is the percentage")
print(ai_percentage)
The problem with this code is mainly that, when there are more than one number that should be marked and retired within any given loop, it only checks the first one before going to the next loop, which means that it always ends with the same amount of loops and numbers that have been marked (while in a lot of cases the number of loops should be smaller). It also makes S a continous sum of all the marked numbers (in the previous example it would do 0.2 + 0.2 + 0.3 + 0.4 + 0.5) rather than keeping the original S constant and just adding the highest marked number, as I have previously explained in the example.
Any ideas or tips regarding this would be appreciated.