lundi 18 janvier 2021

How do i keep my loop from repeating my random values?

So i was learning how to handle probabilities and how to plot them in Python. I came across a problem where i needed to find the probability of the sum of 2 dices being > 7 or odd. I know the result is around 75% but it was translating that to Python that i had a problem with. My code to solve the problem is something like this:

import random
import numpy as np
dice = [1,2,3,4,5,6]
value = 0
simulation_number = len(dice)**2
percentage = []
for i in range(0,simulation_number):
    dice1 = random.choice(dice)
    dice2 = random.choice(dice)
    random_match = dice1,dice2
    
    if (sum(random_match))>7 or (sum(random_match)%2) != 0:
        value += 1
percentage.append(np.round((value/simulation_number)*100,2))
print(percentage,"%")

It works just fine but everytime i run the code it gives a different solution because the loop is repeating outcomes for random_match. How do I include in the code the condition of not repeating random_match values?




Aucun commentaire:

Enregistrer un commentaire