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

< Wright State University Lake Campus < 2019-9

Day 1

####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

Call function

###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

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

##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

##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

program.py

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

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

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

#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

#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

College2006200720082009201020112012201320142015
Belmont Technical College 1714174217982200231920781652131310431082
Central Ohio Technical College 3103311235994377452442813931375835793576
Cincinnati State Tech. & Community College 84388815860610326111331078511042119421171910596
Clark State Community College 3339339235974510501051675636565359695620
Columbus State Community College 22745230562448228539305133092125970253602453226098
Cuyahoga Community College 26340264882781431024319093130130435292672848427850
Eastern Gateway Community College 1601174418312095220924412561292731833025
Edison State Community College 3085307932513357355834593127294630533220
Hocking Technical College 5581525456456193633257864485440939453619
James A. Rhodes State College 3347338036404117419539693460334432812988
Lakeland Community College 8788917292019612999696839447896484108011
Lorain County Community College 10521108241118012783134041293312694122761157211516
Marion Technical College 2043212623502654273527892742269124642440
North Central State College 3195315232573586363533822886295829372938
Northwest State Community College 2881319231853617523844634296465647194551
Owens State Community College 19633210532217823445196401666916903152131249311550
Rio Grande Community College 1528160713861512164916951689173116491579
Sinclair Community College 22951226292346525589260432514424027228532146018610
Southern State Community College 2366243825843363372833522686243124592519
Stark State College of Technology 79208632951612610147091549415252149631356112103
Terra State Community College 2324258226643222356035013192289825402654
Washington State Community College 2316222520812192236522261895178716131550
Zane State College 1934207623122585285629412918365240093209
Community Colleges Total 167693171770179622203508211260204460192926187992178674170904