List: Dice rolling

Dice rolling with lists

 

 

# 
# Roll a six-sided die 6000 times.

import random

frequency1 = 0
frequency2 = 0
frequency3 = 0
frequency4 = 0
frequency5 = 0
frequency6 = 0

for roll in range( 1, 6001 ):       # 6000 die rolls
   face = random.randrange( 1, 7 )

   if face == 1:                    # frequency counted
      frequency1 += 1
   elif face == 2:
      frequency2 += 1
   elif face == 3:
      frequency3 += 1
   elif face == 4:
      frequency4 += 1
   elif face == 5:
      frequency5 += 1
   elif face == 6:
      frequency6 += 1
   else:                            # simple error handling
      print ("should never get here!")

print ("Face %13s" % "Frequency")
print ("   1 %13d" % frequency1)
print ("   2 %13d" % frequency2)
print ("   3 %13d" % frequency3)
print ("   4 %13d" % frequency4)
print ("   5 %13d" % frequency5)
print ("   6 %13d" % frequency6)

##>>> 
##Face     Frequency
##   1          1007
##   2          1036
##   3          1000
##   4          1013
##   5           928
##   6          1016
##>>> 
 

The frequency table for rolling two dice:

#################################################
# mrs.e
# 10/22
# version 1
# Roll two six-sided diec 6000 times.
# Find the sum of the two dice and keep
# track of them and print them out after the
# 6000 times
#
#################################################

import random

def intro():
   print ("Three Rolling Dice Frequency Program")
   print ("This program displays the frequencies")
   print ("of the sum of the faces of two dice")
   print ("when they are rolled 6000 times")
   
def rollDice():
   frequency2 = 0
   frequency3 = 0
   frequency4 = 0
   frequency5 = 0
   frequency6 = 0
   frequency7 = 0
   frequency8 = 0
   frequency9 = 0
   frequency10 = 0
   frequency11 = 0
   frequency12 = 0

   for roll in range( 1, 6001 ):       # 6000 die rolls
      face1 = random.randrange( 1, 7 )
      face2 = random.randrange( 1, 7 )
      totalface = face1 + face2 

      # do not use 
      # facesSum = random.randrange( 2, 13 ) since this will give equal probability to each outcome
      # ...
      if totalface == 2:                    # frequency counted
         frequency2 += 1
      elif totalface == 3:                    
         frequency3 += 1
      elif totalface == 4:
         frequency4 += 1
      elif totalface == 5:
         frequency5 += 1
      elif totalface == 6:
         frequency6 += 1
      elif totalface == 7:
         frequency7 += 1
      elif totalface == 8:
         frequency8 += 1
      elif totalface == 9:
         frequency9 += 1
      elif totalface == 10:
         frequency10 += 1
      elif totalface == 11:
         frequency11 += 1
      elif totalface == 12:
         frequency12 += 1

      else:                            # simple error handling
         print ("should never get here!")
   print ("Sum of Faces %13s" % "Frequency")
   print ("%3s %13d" % ("2",frequency2))
   print ("%3s %13d" % ("3",frequency3))
   print ("%3s %13d" % ("4",frequency4))
   print ("%3s %13d" % ("5",frequency5))
   print ("%3s %13d" % ("6",frequency6))
   print ("%3s %13d" % ("7",frequency7))
   print ("%3s %13d" % ("8",frequency8))
   print ("%3s %13d" % ("9",frequency9))
   print ("%3s %13d" % ("10",frequency10))
   print ("%3s %13d" % ("11",frequency11))
   print ("%3s %13d" % ("12",frequency12))


def main():
   intro()
   rollDice()

main()

The frequency table for rolling three dice:

#################################################
# mrs.e
# 10/22 
# version 1
# Roll three six-sided diec 6000 times.
# Find the sum of the three dice and keep
# track of them and print them out after the
# 6000 times
#
#################################################

import random

def intro():
   print ("Three Rolling Dice Frequency Program")
   print ("This program displays the frequencies")
   print ("of the sum of the faces of three dice")
   print ("when they are rolled 6000 times")
   
def rollDice():
   frequency3 = 0
   frequency4 = 0
   frequency5 = 0
   frequency6 = 0
   frequency7 = 0
   frequency8 = 0
   frequency9 = 0
   frequency10 = 0
   frequency11 = 0
   frequency12 = 0
   frequency13 = 0
   frequency14 = 0
   frequency15 = 0
   frequency16 = 0
   frequency17 = 0
   frequency18 = 0
   for roll in range( 1, 6001 ):       # 6000 die rolls
      face1 = random.randrange( 1, 7 )
      face2 = random.randrange( 1, 7 )
      face3 = random.randrange( 1, 7 )
      totalface = face1 + face2 + face3

      # do not use
      # facesSum = random.randrange( 3, 19 ) since this will give equal probability to each outcome
      # ...

      if totalface == 3:                    # frequency counted
         frequency3 += 1
      elif totalface == 4:
         frequency4 += 1
      elif totalface == 5:
         frequency5 += 1
      elif totalface == 6:
         frequency6 += 1
      elif totalface == 7:
         frequency7 += 1
      elif totalface == 8:
         frequency8 += 1
      elif totalface == 9:
         frequency9 += 1
      elif totalface == 10:
         frequency10 += 1
      elif totalface == 11:
         frequency11 += 1
      elif totalface == 12:
         frequency12 += 1
      elif totalface == 13:
         frequency13 += 1
      elif totalface == 14:
         frequency14 += 1
      elif totalface == 15:
         frequency15 += 1
      elif totalface == 16:
         frequency16 += 1
      elif totalface == 17:
         frequency17 += 1
      elif totalface == 18:
         frequency18 += 1
      else:                            # simple error handling
         print ("should never get here!")
   print ("Sum of Faces %13s" % "Frequency")
   print ("%3s %13d" % ("3",frequency3))
   print ("%3s %13d" % ("4",frequency4))
   print ("%3s %13d" % ("5",frequency5))
   print ("%3s %13d" % ("6",frequency6))
   print ("%3s %13d" % ("7",frequency7))
   print ("%3s %13d" % ("8",frequency8))
   print ("%3s %13d" % ("9",frequency9))
   print ("%3s %13d" % ("10",frequency10))
   print ("%3s %13d" % ("11",frequency11))
   print ("%3s %13d" % ("12",frequency12))
   print ("%3s %13d" % ("13",frequency13))
   print ("%3s %13d" % ("14",frequency14))
   print ("%3s %13d" % ("15",frequency15))
   print ("%3s %13d" % ("16",frequency16))
   print ("%3s %13d" % ("17",frequency17))
   print ("%3s %13d" % ("18",frequency18))

def main():
   intro()
   rollDice()

main()

Some of you want to use lists. Here is a good program with lists:

#Alex Ding
#11/1
#2.7
#test

print "This program calculates the frequencies of the sum of 3 die in 6000 rolls."

frequency = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

import random

def sumoffaces():       
   face1 = random.randrange(1,7)
   face2 = random.randrange(1,7)
   face3 = random.randrange(1,7)
   face = face1 + face2 + face3
   return face

def calc(face):
   if face == 3:                    
      frequency[0] += 1
   elif face == 4:
      frequency[1] += 1
   elif face == 5:
      frequency[2] += 1
   elif face == 6:
      frequency[3] += 1
   elif face == 7:
      frequency[4] += 1
   elif face == 8:
      frequency[5] += 1
   elif face == 9:
      frequency[6] += 1
   elif face == 10:
      frequency[7] += 1
   elif face == 11:
      frequency[8] += 1
   elif face == 12:
      frequency[9] += 1
   elif face == 13:
      frequency[10] += 1
   elif face == 14:
      frequency[11] += 1
   elif face == 15:
      frequency[12] += 1
   elif face == 16:
      frequency[13] += 1
   elif face == 17:
      frequency[14] += 1
   elif face == 18:
      frequency[15] += 1
   else:                            # simple error handling
      print "should never get here!"

def printing():
   face = 3
   print ("Face %13s" % "Frequency")
   for face in range (3,19):
      print ("%4d%13d" % (face,frequency[face-3]))


def main():
   frequency1 = 0
   frequency2 = 0
   frequency3 = 0
   frequency4 = 0
   frequency5 = 0
   frequency6 = 0
   for roll in range(1,6001):
      face = sumoffaces()
      calc(face)   
   printing()

main()

##>>> 
##This program calculates the frequencies of the sum of 3 die in 6000 rolls.
##Face     Frequency
##   3           29
##   4           76
##   5          151
##   6          275
##   7          437
##   8          617
##   9          673
##  10          755
##  11          719
##  12          687
##  13          606
##  14          399
##  15          288
##  16          178
##  17           74
##  18           36
##>>> 

Here is a shorter version:

#################################################
# mrs.e
# 10/22
# version 2
# Roll three six-sided diec 6000 times.
# Find the sum of the three dice and keep
# track of them and print them out after the
# 6000 times
#
#################################################

import random

frequency = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

def intro():
   print ("Three Rolling Dice Frequency Program")
   print ("This program displays the frequencies")
   print ("of the sum of the faces of three dice")
   print ("when they are rolled 6000 times")
   
def rollDice():
   for roll in range( 1, 6001 ):       # 6000 die rolls
      face1 = random.randrange( 1, 7 )
      face2 = random.randrange( 1, 7 )
      face3 = random.randrange( 1, 7 )
      totalface = face1 + face2 + face3
      frequency[totalface] +=1

   print "Sum of Faces %13s" % "Frequency"
   for face in range(3,19):
      print ("%3d %13d" % (face,frequency[face]))


def main():
   intro()
   rollDice()

main()

###################################################
#
#   Adrian Hertel
#   Three dice rolls: 6000 times
#   python 2.7
#   11/1/2013
#
##################################################



import random

totals = [0] * 16

def increment(total):
      totals[total-3] += 1
      
def roll3Dice():
      total = 0
      for i in range (0,3):
         total += random.randrange( 1, 7 )
      increment(total)

def main():
   for roll in range( 1, 6001 ):
      roll3Dice()
   print ("Sum %13s" % "Frequency")
   for x in range(0, len(totals)):
      print ("%3d %13d" % (x + 3, totals[x]))
      
main()


##>>> 
##Sum     Frequency
##  3            19
##  4            95
##  5           169
##  6           281
##  7           400
##  8           580
##  9           715
## 10           745
## 11           765
## 12           729
## 13           560
## 14           410
## 15           272
## 16           166
## 17            72
## 18            22
##>>> 

Let’s take a look at lists:

Khan Academy on python lists

Homework: Use lists for your own rolling dice program. If your program already has a list and works, make it efficient. If you program is already efficient, make it even more efficient by eliminating these many variables:

      face1 = random.randrange( 1, 7 )
      face2 = random.randrange( 1, 7 )
      face3 = random.randrange( 1, 7 )
      totalface = face1 + face2 + face3
      frequency[totalface] +=1