samedi 11 janvier 2020

Calculating possible outcomes from the random module

My code should represent this assignment:

You have a bag containing nine balls. Of these, three are blue, three are red and three are green. You should write a program that simulates that you try to remove from the bag three balls of the same color (without adding the ball back). The program shall also redo the experiment over and over to show how probability and favorable outcome are related to possible outcomes. Repeat the experiment 10 times for 100, 1000 and 10000 draws, ie 10 rounds with 100 draws, 10 with 1000 draws, and 10 with 10000 draws. Print the result with a well-formatted printout.

from random import randint
getlucky = 0                                    
draught = 1                                    
for _ in range (10000):                           
    reds = 0                                    
    blues = 0
    greens = 0
    balls = [1, 2, 3, 4, 5, 6, 7, 8, 9]         
    for _ in range(3):                          
        value = randint(0, len(balls)-1)        
        if balls[value] % 3 == 0:               

            reds += 1                           
        elif balls[value] % 3 == 1:             
            blues += 1                          
        elif balls[value] % 3 == 2:             
            greens += 1                         
        balls.remove(balls[value])             
    print("Dragning nr", draught ,":", reds, "röda,", blues, "blåa,", greens, "gröna")
    draught += 1                               
    if reds == 3 or greens == 3 or blues == 3:  
        getlucky += 1
    procent = getlucky                         
print(procent,"% gynnsamma utfall")

Although something is off with the code, since when I enter 10'000 as the amount of draughts the result of possible outcomes is always around 350%. What could be the problem?




Aucun commentaire:

Enregistrer un commentaire