Wright State University Lake Campus/2019-9/Python dice

Day 1 edit

####lesson 1: How to find a random integer betwee one and six
### First step: Google with {python random integer}
###https://www.pythoncentral.io/how-to-generate-a-random-number-in-python/
##import random 
##for x in range(10):
##    print(random.randint(1,6))

# lesson 2 Let the variable Nthrow denote the number of dice to throw.
##import random

##Nthrow=10 #We need to control howm many dice are thrown
##for nThrow in range(Nthrow):
##    if random.randint(1,6)==6:
##        print("decay")
##    else:
##        print("nodecay")

### lesson 3 Let the variable Nthrow denote the number of dice to throw.
##import random
##Nthrow=10 #We need to control howm many dice are thrown
##for nThrow in range(Nthrow):
##    if random.randint(1,6)==6:
##        print("decay")
##    else:
##        print("nodecay")

# lesson 4 Count the number that survived and decayed:
##import random
##Nthrow=10 #We need to control how many dice are thrown
##nDecayed=0 #initial count
##nSurvived=0 #initial ocount
##for nThrow in range(Nthrow):
##    if random.randint(1,6)==6:
##        nDecayed+=1 #fancy way to add 1 to nDecayed
##        print("Decayed:",nDecayed)        
##    else:
##        nSurvived+=1 #adds on to number that survived
##        print("Survived:",nSurvived)
##print(nDecayed,'decayed\n', nSurvived,'survived')

# lesson 5 Turn this into a function so we don't need to look at it:
#Google {python simple function return variable}
#https://www.python-course.eu/python3_functions.php
#Warning: Don't use the Docstring in main function (considered a bad habit)

Day 2 edit

Call function edit

###From lesson 4 of Day 1 we have:
#####Lesson 1
##import random
##Nthrow=10 #We need to control how many dice are thrown
##nDecayed=0 #initial count
##nSurvived=0 #initial ocount
##for nThrow in range(Nthrow):
##    if random.randint(1,6)==6:
##        nDecayed+=1 #fancy way to add 1 to nDecayed
##        print("Decayed:",nDecayed)        
##    else:
##        nSurvived+=1 #adds on to number that survived
##        print("Survived:",nSurvived)
##print(nDecayed,'decayed\n', nSurvived,'survived')

### lesson 2 Turn this into a function so we don't need to look at it:
###Google {python simple function return variable}
###https://www.python-course.eu/python3_functions.php
###Warning: Don't use the Docstring in main function (considered a bad habit)
##import random
##def throwSomeDice(Nthrow):
##    print("Inside function throwSomeDice, with", Nthrow,"dice")
##
##    nDecayed=0 #initial count
##    nSurvived=0 #initial ocount
##    for nThrow in range(Nthrow):
##        rolledThis=random.randint(1,6)
##        if rolledThis==6:
##            nDecayed+=1 #fancy way to add 1 to nDecayed
##            print("Rooled",rolledThis,"Decayed:",nDecayed)        
##        else:
##            nSurvived+=1 #adds on to number that survived
##            print("Rooled",rolledThis,"Survived:",nSurvived)
##    print(nDecayed,'decayed\n', nSurvived,'survived')
##    print("Leaving function throwSomeDice")
##    return nSurvived
##print(throwSomeDice(10), 'are left')

Day 3 edit

Today we "hide" throwSomeDice from our program to keep it short. This will require two python functions: program.py and diceFunctions.py in the same folder. Perhaps call the folder "dice"?

Later we will attempt to graph using https://www.python-course.eu/tkinter_canvas.php

From Day 2 we modify slightly:

program.py edit

##import random
##def throwSomeDice(Nthrow):
##    print("Inside function throwSomeDice, with", Nthrow,"dice")
##
##    nDecayed=0 #initial count
##    nSurvived=0 #initial ocount
##    for nThrow in range(Nthrow):
##        rolledThis=random.randint(1,6)
##        if rolledThis==6:
##            nDecayed+=1 #fancy way to add 1 to nDecayed
##            print("Rooled",rolledThis,"Decayed:",nDecayed)        
##        else:
##            nSurvived+=1 #adds on to number that survived
##            print("Rooled",rolledThis,"Survived:",nSurvived)
##    print(nDecayed,'decayed\n', nSurvived,'survived')
##    print("Leaving function throwSomeDice")
##    return nSurvived
##print(throwSomeDice(10), 'are left')

diceFunctions.py edit

##def throwSomeDice(Nthrow):
##    print("Inside function throwSomeDice, with", Nthrow,"dice")
##
##    nDecayed=0 #initial count
##    nSurvived=0 #initial ocount
##    for nThrow in range(Nthrow):
##        rolledThis=random.randint(1,6)
##        if rolledThis==6:
##            nDecayed+=1 #fancy way to add 1 to nDecayed
##            print("Rooled",rolledThis,"Decayed:",nDecayed)        
##        else:
##            nSurvived+=1 #adds on to number that survived
##            print("Rooled",rolledThis,"Survived:",nSurvived)
##    print(nDecayed,'decayed\n', nSurvived,'survived')
##    print("Leaving function throwSomeDice")
##    return nSurvived

If you get stuck just use these edit

program.py edit

from diceFunctions import throwSomeDice
Nsurvived_string= input('How many dice shall we start with? ')
Nsurvived=int(Nsurvived_string)
while Nsurvived>0:
    print(Nsurvived)
    nextN=throwSomeDice(Nsurvived)
    Nsurvived=nextN

diceFunctions.py edit

import random
def throwSomeDice(Nthrow):
    '''Throws Nthrow dice and returns the number of sixes'''

    nDecayed=0 #initial count
    nSurvived=0 #initial ocount
    for nThrow in range(Nthrow):
        rolledThis=random.randint(1,6)
        if rolledThis==6:
            nDecayed+=1 #fancy way to add 1 to nDecayed
            print("Rooled",rolledThis,"Decayed:",nDecayed)        
        else:
            nSurvived+=1 #adds on to number that survived
            print("Rooled",rolledThis,"Survived:",nSurvived)
    #print(nDecayed,'decayed\n', nSurvived,'survived')
    #print("Leaving function throwSomeDice")
    return nSurvived

Tkinter 1 edit

Both codes belong in same folder. Run program.py.

Current convigurations shows experiment in red and 40 simulations in black. Dotted blue is the theoretical average decay.

It also fun to try: HALF= 200 and SHIFT=.01

program.py edit

#program.py
#http://zetcode.com/tkinter/drawing/
from tkinter import Tk, Canvas, Frame, BOTH
from diceFunctions import throwSomeDice

experiment=[22, 21, 21, 16, 10, 11, 6]

theory=[]
for i in range(len(experiment)):
    theory.append( experiment[0] * (5/6)**i  )
baseline=[0,0,0,0,0,0,0]

def simulate(experiment):
    Ndice=experiment[0]
    Nthrows=len(experiment)-1
    output=[Ndice]
    for i in range(Nthrows):
        Ndice=throwSomeDice(Ndice)
        output.append(Ndice)
    return output

def makeProcessed(raw,count):
    output=[]
    for i in range(len(raw)):
        output.append(100+100*i)
        output.append(24*30-30*raw[i]-count)
    return output

class Example(Frame):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        ###############################
        SHIFT=.15#.25 #vertical shift to delete overlap
        HALF=20#10 #Make 2*HALF graphs       
        #################################
        self.master.title("Lines")
        self.pack(fill=BOTH, expand=1)
        canvas = Canvas(self)
        canvas.create_line(makeProcessed(baseline,0),dash=(4,2))
        shift=0
        for count in range(2*HALF):
            shift+=SHIFT
            simData=simulate(experiment)
            canvas.create_line(makeProcessed(simData,shift))

        canvas.create_line(makeProcessed(experiment,HALF*SHIFT),fill="red")
        canvas.create_line(makeProcessed(theory,HALF*SHIFT),
           fill='blue', dash=(2,1))        
        canvas.pack(fill=BOTH, expand=1)       
def main():
    root = Tk()
    ex = Example()
    root.geometry("3000x800+5+5")
    root.mainloop()

if __name__ == '__main__':
    main()

diceFunctions.py edit

#diceFunctions.py 
import random
def throwSomeDice(Nthrow):
    '''Throws Nthrow dice and returns the number of sixes'''

    nDecayed=0 #initial count
    nSurvived=0 #initial ocount
    for nThrow in range(Nthrow):
        rolledThis=random.randint(1,6)
        if rolledThis==6:
            nDecayed+=1 #fancy way to add 1 to nDecayed
            #print("Rooled",rolledThis,"Decayed:",nDecayed)        
        else:
            nSurvived+=1 #adds on to number that survived
            #print("Rooled",rolledThis,"Survived:",nSurvived)
    #print(nDecayed,'decayed\n', nSurvived,'survived')
    #print("Leaving function throwSomeDice")
    return nSurvived

College enrollment noise edit

College 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
Belmont Technical College 1714 1742 1798 2200 2319 2078 1652 1313 1043 1082
Central Ohio Technical College 3103 3112 3599 4377 4524 4281 3931 3758 3579 3576
Cincinnati State Tech. & Community College 8438 8815 8606 10326 11133 10785 11042 11942 11719 10596
Clark State Community College 3339 3392 3597 4510 5010 5167 5636 5653 5969 5620
Columbus State Community College 22745 23056 24482 28539 30513 30921 25970 25360 24532 26098
Cuyahoga Community College 26340 26488 27814 31024 31909 31301 30435 29267 28484 27850
Eastern Gateway Community College 1601 1744 1831 2095 2209 2441 2561 2927 3183 3025
Edison State Community College 3085 3079 3251 3357 3558 3459 3127 2946 3053 3220
Hocking Technical College 5581 5254 5645 6193 6332 5786 4485 4409 3945 3619
James A. Rhodes State College 3347 3380 3640 4117 4195 3969 3460 3344 3281 2988
Lakeland Community College 8788 9172 9201 9612 9996 9683 9447 8964 8410 8011
Lorain County Community College 10521 10824 11180 12783 13404 12933 12694 12276 11572 11516
Marion Technical College 2043 2126 2350 2654 2735 2789 2742 2691 2464 2440
North Central State College 3195 3152 3257 3586 3635 3382 2886 2958 2937 2938
Northwest State Community College 2881 3192 3185 3617 5238 4463 4296 4656 4719 4551
Owens State Community College 19633 21053 22178 23445 19640 16669 16903 15213 12493 11550
Rio Grande Community College 1528 1607 1386 1512 1649 1695 1689 1731 1649 1579
Sinclair Community College 22951 22629 23465 25589 26043 25144 24027 22853 21460 18610
Southern State Community College 2366 2438 2584 3363 3728 3352 2686 2431 2459 2519
Stark State College of Technology 7920 8632 9516 12610 14709 15494 15252 14963 13561 12103
Terra State Community College 2324 2582 2664 3222 3560 3501 3192 2898 2540 2654
Washington State Community College 2316 2225 2081 2192 2365 2226 1895 1787 1613 1550
Zane State College 1934 2076 2312 2585 2856 2941 2918 3652 4009 3209
Community Colleges Total 167693 171770 179622 203508 211260 204460 192926 187992 178674 170904