I am just starting in Python (though have coding experience in other languages) and am working on this assignment for a class in ArcGIS pro. We have been tasked with using the random module to generate a list of random numbers between 0 and 1 with a few different 'cutoffs' (shown here 0.7) and 'counts' shown here 5.
I have tried a few different ways of approaching this problem including using nested loops (shown below, different count and cutoff) but came out with a list too long. I am now trying to approach the problem using only a while loop after some guidance from my professor, yet my list remains... empty? Any help is appreciated.
You have the right idea, just some bugs in your code. The first one, your x
is just counting from 0
to count
and that's what you are appending to your list of numbers rather than the random.random()
output. Also, you didn't assign any value to your random.random()
output either.
The second example is much closer to what you're looking for, but you are missing a colon after the if
statement and the numbers.append(x)
should be indented.
import random
seed = 37
cutoff = 0.4
count = 8
random.seed(seed)
numbers = []
while len(numbers) < count:
x = random.random()
if x > cutoff:
numbers.append(x)
print(numbers)