Il giorno mercoledì 6 dicembre 2017 02:33:52 UTC+1, nick martinez ha scritto:
> I have a question on my homework. My homework is to write a program in which 
> the computer simulates the rolling of a die 50
> times and then prints
> (i). the most frequent side of the die
> (ii). the average die value of all rolls. 
> I wrote the program so it says the most frequent number out of all the rolls 
> for example (12,4,6,14,10,4) and will print out "14" instead of 4 like I need.
> This is what I have so far:
> import random
> 
> def rollDie(number):
>     rolls = [0] * 6
>     for i in range(0, number):
>         roll=int(random.randint(1,6))
>         rolls[roll - 1] += 1
>     return rolls
> 
> if __name__ == "__main__":
>     result = rollDie(50)
>     print (result)
>     print(max(result))

Another way to get an answer is to use the numpy package. It is freely 
available and it is the "de facto" standard for numerical problems.
If you get python from one of the scientific distribution (Anaconda, enthought, 
etc.) it will be automatically installed.

  import numpy

  Nrolls = 50     # but you can easily have millions of rolls 
  a = numpy.random.randint(1,7,Nrolls)

And then to count the rolls for each number you can use the histogram function 

  frequency , _ = numpy.histogram(a, bins=[1,2,3,4,5,6,7])

You can now use the "argmax" method to find the number that appeared most 
frequently. Use the "mean" method to calculate the mean. Look on the help pages 
of numpy why I used radint(1,7) instead of radint(1,6) and the same for the 
bins in the histogram function.

Have a look at: www.scipy.org

Cheers :-)
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to