Dec-02-2018, 02:15 PM
Hello, I have an exercise where I have to calculate the probability from rolling a dice. I do have the probability from 1 side, but I need the probabality from all sides. So my problem is generalizing the code to make it work. This is what I have so far
import random def prob_die(max_exp, outcome): counter = 0 for _ in range(max_exp): s = random.randint(1,6) # roll if s==outcome: counter +=1 return counter/max_exp prob_die(100000, 6)I was looking at one example which is this one
import random random.seed() ROLLED = {i: 0 for i in range(1, 7)} ITERATIONS = int(input('How many times would you like to roll the dice? ')) def probability(): print("Calculation of probability: ") for key, count in ROLLED.items(): print("\t{}: {:.2f}".format(key, count*100./ITERATIONS*1.)) for _ in range(ITERATIONS): ROLLED[random.randint(1, 6)] += 1 probability()But I can't seem to understand it quite well. Some help would be appreciated!