summing the dice trials and histogram plot

前端 未结 3 1799
终归单人心
终归单人心 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 03:59

    Try this,

    from random import choice
    import pylab
    
    def roll( rolls, dice ):
    s = list()
    for d in range( dice ):
        for r in range( rolls ):
            s.append( choice( range(1,7) ) )
    return s
    
    s = roll( rolls, dice )
    
    sum_of_rolls = sum( s )
    
    # then to plot..
    pylab.hist( s )
    

提交回复
热议问题