summing the dice trials and histogram plot

前端 未结 3 1824
终归单人心
终归单人心 2021-01-28 03:21

I am stuck in a code in python which takes in number of dices and number of rolls and returns the sum of numbers obtained. It should also print the histogram of the sum. I am st

3条回答
  •  执念已碎
    2021-01-28 04:10

    This should do it

    import random
    
    def rolls(N, r):  # N=number of dice. r=number of rolls
        myDie = [1,2,3,4,5,6]
        answer = {}
        for _rolling in range(r):
            rolls = []
            for _die in range(N):
                rolls.append(random.choice(myDie))
            total = 0
            for roll in rolls:
                total += roll
            if total not in answer:
                answer[total] = 0
            answer[total] += 1
        return answer
    

提交回复
热议问题