SpliceFire
SpliceFire

Reputation: 11

While and for statements iterating wrong number of times

My question is why is it repeating random amount of times instead of 100 times ? The print (len(result)) was added just to check how many iterations did it actually go over, and it's always a random number instead of 100 times.

I also tried using for i in range(100) but it didn't work as well.

# Die simulation

import random
min = 1 
max = 6

counter_1 = 0
counter_2 = 0
counter_3 = 0
counter_4 = 0
counter_5 = 0
counter_6 = 0

i = 0
result = []

while i < 100:
    if random.randint(min,max) == 1:
        print ("The die landed on 1.")
        counter_1 +=1
        result.append(1)

    elif random.randint(min,max) == 2:
        print ("The die landed on 2.")
        counter_2 +=1
        result.append(2)

    elif random.randint(min,max) == 3:
        print ("The die landed on 3.")
        counter_3 +=1
        result.append(3)

    elif random.randint(min,max) == 4:
        print ("The die landed on 4.")
        counter_4 +=1
        result.append(4)

    elif random.randint(min,max) == 5:
        print ("The die landed on 5.")
        counter_5 +=1
        result.append(5)

    elif random.randint(min,max) == 6:
        print ("The die landed on 6.")
        counter_6 +=1
        result.append(6)


    i +=1

print (result)
print (len(result))
print ("In a total of", 100, "number of simulations, The die landed on 1", counter_1, "times, landed on 2", counter_2, "times, landed on 3", counter_3 ,"times, ", end = '')
print ("landed on 4" , counter_4, "times, landed on 5", counter_5, "times, landed on 6", counter_6, "times.")

Upvotes: 0

Views: 76

Answers (3)

Patrick Artner
Patrick Artner

Reputation: 51683

As Robin Zigmond answered: you are recreating the random so neither condition might match.

Fix:

while i < 100:
    random_number =  random.randint(min,max)

    if random_number == 1:
        print ("The die landed on 1.")
        counter_1 +=1
        result.append(1)

    elif random_number == 2:
        print ("The die landed on 2.")
        counter_2 +=1
        result.append(2)

    elif random_number == 3:
        print ("The die landed on 3.")
        counter_3 +=1
        result.append(3)

    elif random_number == 4:
        print ("The die landed on 4.")
        counter_4 +=1
        result.append(4)

    elif random_number == 5:
        print ("The die landed on 5.")
        counter_5 +=1
        result.append(5)

    elif random_number == 6:            # else: would suffice
        print ("The die landed on 6.")
        counter_6 +=1
        result.append(6) 

    i +=1

Your die-simulator is overly compex. random can generate a list of 100 dice throws at once. You can use the Counter class to count them easily:

import random
min_value = 1  # min and max are already taken as names by built ins
max_value = 6

# create 100 random numbers 1 to 6 - range upper bound is exlusive, hence max_value+1
dices = random.choices(range(min_value,max_value+1), k= 100)

from collections import Counter

# create a dict that counts what dices happened how often
numbers = Counter(dices)
for die, count in  sorted(numbers.most_common()):
    print(f"Die {die} occured {count} times.")


print ("In a total of", 100, "number of simulations, The die landed on 1", 
       numbers[1], "times, landed on 2", numbers[2], "times, landed on 3", 
       numbers[3],"times, landed on 4" , numbers[4], "times, landed on 5",
       numbers[5], "times, landed on 6", numbers[6], "times.")

Output:

Die 1 occured 13 times.
Die 2 occured 18 times.
Die 3 occured 15 times.
Die 4 occured 17 times.
Die 5 occured 20 times.
Die 6 occured 17 times.
In a total of 100 number of simulations, The die landed on 1 13 times, 
landed on 2 18 times, landed on 3 15 times, landed on 4 17 times, 
landed on 5 20 times, landed on 6 17 times.

Readup:

Upvotes: 0

Jim Todd
Jim Todd

Reputation: 1588

That's right. random.randint should be put once within the loop like below:

import random
min = 1 
max = 6

counter_1 = 0
counter_2 = 0
counter_3 = 0
counter_4 = 0
counter_5 = 0
counter_6 = 0

i = 0
result = []

while i < 100:
    holder=random.randint(min,max)
    if holder == 1:
        print ("The die landed on 1.")
        counter_1 +=1
        result.append(1)

    elif holder== 2:
        print ("The die landed on 2.")
        counter_2 +=1
        result.append(2)

    elif holder == 3:
        print ("The die landed on 3.")
        counter_3 +=1
        result.append(3)

    elif holder == 4:
        print ("The die landed on 4.")
        counter_4 +=1
        result.append(4)

    elif holder == 5:
        print ("The die landed on 5.")
        counter_5 +=1
        result.append(5)

    elif holder == 6:
        print ("The die landed on 6.")
        counter_6 +=1
        result.append(6)


    i +=1

print (result)
print (len(result))
print ("In a total of", 100, "number of simulations, The die landed on 1", counter_1, "times, landed on 2", counter_2, "times, landed on 3", counter_3 ,"times, ", end = '')
print ("landed on 4" , counter_4, "times, landed on 5", counter_5, "times, landed on 6", counter_6, "times.")

Upvotes: 1

Robin Zigmond
Robin Zigmond

Reputation: 18249

The problem is that you're calling random.randint on each if and elif check - with the result that it's possible that none of the checks pass, resulting in fewer than 100 numbers in your final array. You should call this function just once per iteration, store the result in a variable, and do the checks on that.

Upvotes: 3

Related Questions