On 12/05/2017 07:33 PM, nick.martinez2--- via Python-list wrote:
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.

How did you come up with 4?  I come up with 3.36 with those numbers.

This is what I have so far:
import random

def rollDie(number):
     rolls = [0] * 6

For a small efficiency use "[0] * 7".  See below for reason.

     for i in range(0, number):
         roll=int(random.randint(1,6))
         rolls[roll - 1] += 1

Use "rolls[roll] += 1" here. You save one arithmetic instruction each time through the loop. Fifty times isn't much saving but imagine fifty million.

     return rolls

return rolls[1:]. No matter how many rolls you only need to do the slice once. However, you really need a lot of iterations before it really affects the total run time.

You need one more thing though. Create a new variable called "total" and set it to 0.0. Add "total += roll" to your loop. your return statement is now "return rolls[1:], total/number".


if __name__ == "__main__":
     result = rollDie(50)

Now "result, average = ..."

     print (result)
     print(max(result))

This is why you get 14. The maximum number of rolls for any one side is 14 for side 4. Is that where you got "4"?

--
D'Arcy J.M. Cain
Vybe Networks Inc.
http://www.VybeNetworks.com/
IM:da...@vex.net VoIP: sip:da...@vybenetworks.com
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to