︠196df29c-d20c-4922-bdf0-53a5415f061fs︠
#random number
random()
︡e4049a56-e7ab-414e-9108-63bba5b51390︡{"stdout":"0.4430272557901488\n"}︡{"done":true}︡
︠c55e83b1-2428-4596-9577-d4293056c566s︠
#random integer
randint(9,13)
︡72f73c5f-c2bf-4fbd-98e9-cee3f4b6cdee︡︡{"stdout":"11\n"}︡{"done":true}
︠2fc27aae-f8ee-4ea1-83ee-10df75e98adf︠
#make a table of random stuff
for _ in range(10):
print randint(1,6)
︡6e452ec6-c5d1-41b3-839b-6553aa6abc27︡
︠c23fe4c6-a028-46a8-8c5e-f236b7f8fades︠
#make a list of random stuff
myDecimalData = [random() for _ in range(100)]
myDice = [randint(1,6) for _ in range(10000)]
︡2128b126-b848-4082-b41a-30596038d4d6︡{"done":true}︡
︠07a1bacf-eec7-4941-b85d-466d8303f78e︠
myDice
︡a76343eb-059e-472f-85e7-3db729a28f98︡
︠b476439b-0e82-46c2-af81-508750b84111︠
︡39a83557-5b4b-4e9b-9d23-665ae720d4b7︡
︠87ecee6f-adc2-4521-a52d-af65eaa0daae︠
︡23f25243-0250-4c7e-bdfb-fa535a90466c︡
︠55d40046-af43-4c0b-8a6d-33cca16ef5fds︠
#do stats with lists
mean(myDice)
︡f5c428ef-53a4-4279-a231-6f9f8ed6c4cf︡{"stdout":"8833/2500\n"}︡{"done":true}︡
︠874d08bd-5e9d-419d-9d12-ed52f84aabffs︠
#get MAD!
def MAD(myList):
m = mean(myList)
absDevs=[abs(x-m) for x in myList]
return mean(absDevs)
︡2ace209d-a0f4-4da6-86b7-382100fc5f44︡︡{"done":true}
︠4ef36dc1-cf0f-4935-bf1f-fd9cb3a5b807︠
MAD(myDice)
︡35193e81-2d7e-4c11-b129-3e3999922d24︡{"stdout":"1767/1250\n"}︡
︠6a750f99-d4f0-4720-be82-ddfb4e59e204s︠
histogram(myDice, bins=[0..7],align='left')
︡3d0860d0-d87a-4f99-9472-e7418a1a8830︡{"file":{"filename":"/projects/dc288336-f2cd-4333-8eaf-56382a3bcf40/.sage/temp/compute1-us/31571/tmp_wX2igv.svg","show":true,"text":null,"uuid":"4062018b-4f55-4a60-bae3-3e7fc72eca71"},"once":false}︡{"html":"
"}︡{"done":true}︡
︠6848b3df-8b3a-4e17-b4e3-ea6e16098ae1︠
[myDice.count(k) for k in [1..6]]
︡66ee5cd8-00f0-48a5-ac69-dee390895132︡{"stdout":"[1632, 1636, 1660, 1687, 1688, 1697]\n"}︡
︠eea715da-91f0-4d5b-8166-7f34e8faa4dd︠
#sums of dice
sumDice = [randint(1,6)+randint(1,6) for _ in range(100000)]
︡ea15a46e-64b0-4d19-a8d7-cafd04521804︡
︠eb962069-5b25-4c99-bcf8-dc5ff222b51f︠
histogram(sumDice,bins=[0..13],align='left')
︡67bed830-a4bc-4c68-8f3a-fa1c1611a49c︡{"once":false,"file":{"show":true,"uuid":"f8bc9407-5773-4184-b050-8f0aff3d4f01","filename":"/projects/dc288336-f2cd-4333-8eaf-56382a3bcf40/.sage/temp/compute2-us/3834/tmp_gIPCmV.svg"}}︡{"html":""}︡
︠4f559821-7df7-4c8c-8af6-20b14509f427︠
sumsOfMany = [tossDice(10) for _ in range(10000)]
histogram(sumsOfMany,bins=[0..61],align='left')
︡d87cce50-d25e-4e84-b182-6d892c967fe8︡{"once":false,"file":{"show":true,"uuid":"454b8822-a3b8-4887-b4bf-b19567ab0ad4","filename":"/projects/dc288336-f2cd-4333-8eaf-56382a3bcf40/.sage/temp/compute2-us/3834/tmp__4a9AV.svg"}}︡{"html":""}︡
︠30df8ad4-e90a-45f8-b9b1-51ff1ca2282es︠
#sums of many dice
def tossDice(nTosses):
total = 0
for _ in range(nTosses):
total += randint(1,6) #increment total
return total
︡f3b49334-bb53-4f2d-85cd-46ea27e8c0a9︡{"done":true}︡
︠739f2bf5-38ec-48a5-99f4-3b901b257425s︠
tossDice(500)
︡2ee63e1a-8036-4718-8b26-f1c341e19569︡{"stdout":"1756\n"}︡{"done":true}︡
︠1f39e6de-3f36-4476-96ec-9b5d9d1a26d8︠
#counting heads
#simulate tossing a coin several times and counting heads
#simulate a coin with a random number that is either 0 or 1
#heads = 1, tails = 0
def count_heads(nFlips):
outcomes=[randint(0,1) for _ in range(nFlips)]
return sum(outcomes)
︡5bb5d768-f75b-4203-8d04-4689c7cd7930︡
︠110bb8cb-27e6-4360-af66-01d879beb4d0︠
count_heads(100000)
︡1638d1e4-7142-403a-a8b7-6ad5e19efee6︡{"stdout":"49890"}︡{"stdout":"\n"}︡
︠45147195-fcf1-4b9e-bc65-01fd4b59195d︠
#Now do many trials
nFlips = 1000
nTrials = 10000
coinData = [count_heads(nFlips) for _ in range(nTrials)]
histogram(coinData,bins=[0..nFlips+1],align='left',xmin=-1)
print float(MAD(coinData)/nFlips)
︡3f841f4d-8243-4d5d-a3d2-c9c6713a8f6b︡{"once":false,"file":{"show":true,"uuid":"e4d0a2da-bfe6-4ffd-a965-92c36c3f6d23","filename":"/projects/dc288336-f2cd-4333-8eaf-56382a3bcf40/.sage/temp/compute2-us/3834/tmp_UkiaZD.svg"}}︡{"html":""}︡{"stdout":"0.01245004704\n"}︡
︠6bda2ab4-b2b3-4031-b02f-38fb1c6da0e2︠
#check how data deviates from mean
sum([coinData.count(k) for k in [4..6]])
︡68306385-84e5-4860-b893-874a05f19cb5︡{"stderr":"Error in lines 2-2\nTraceback (most recent call last):\n File \"/projects/dc288336-f2cd-4333-8eaf-56382a3bcf40/.sagemathcloud/sage_server.py\", line 879, in execute\n exec compile(block+'\\n', '', 'single') in namespace, locals\n File \"\", line 1, in \nNameError: name 'coinData' is not defined\n"}︡
︠925ab8f6-6131-4f7e-88f9-a67c84944466︠
#examine coin statistics for streaks
#count maximum streak in list
def streak(myList):
start = myList[0]
streakLength = 1
maxStreakLength = 1
for a in myList[1:]:
if a == start:
streakLength += 1
if streakLength>maxStreakLength:
maxStreakLength = streakLength
else:
start = a
streakLength = 1
return maxStreakLength
#fair coin
nTosses = 100
nTrials = 1000
#1=H, 0=T
#outcome = [randint(0,1) for _ in range(nTosses)]
streaks = [streak([randint(0,1) for _ in range(nTosses)]) for _ in range(nTrials)]
#outcomes = [[randint(0,1) for _ in range(nTosses)] for _ in range(nTrials)]
#for r in range(nTrials):
#rOutcome = outcomes[r]
#print rOutcome, streak(rOutcome)
meanS = float(mean(streaks))
minS = min(streaks)
maxS = max(streaks)
print('%s trials of flipping a coin %s times'%(nTrials,nTosses))
histogram(streaks,bins=range(minS-1,maxS+2),align='left',alpha=0.8,color='green')
print('Minimum streak length is %s, maximum is %s, mean is %s'%(minS,maxS,meanS))
︡55dfeaad-fbe8-4f03-b2ae-6df6654a7a53︡
︠3633a0fa-2eca-4da7-ad7e-7e05e78213d9s︠
︡4842e574-de02-4621-bc33-e584cb4ddfdc︡︡{"done":true}
︠b353013a-d1a5-4260-9a9a-7662ec95defe︠
︡8a73c4a7-22df-44ab-a56d-bf247740bb1a︡
︠1ce13bef-0139-47e2-b30a-acfc4c940b01︠
︡549eaf92-7291-4b9b-b3e5-e1d8786c6f68︡
︠6ef8c9ee-96dd-4ac3-a272-2527c96a24dd︠
︡1735421e-ce43-49b0-9315-ace372ff7b55︡
︠a2c83d9c-ca59-4ac7-8e16-740bb06f2020s︠
#simulate selection of card from deck
#and check whether there are facecards or not
vals=['A','2','3','4','5','6','7','8','9','10','J','Q','K']
suits = ['\color{red}\diamondsuit','\clubsuit','\color{red}\heartsuit','\spadesuit']
nVals = len(vals)
nSuits = len(suits)
nCards = nVals*nSuits
nTrials = 10
nDealt = 3 #number of cards to deal
nFacecards = 0 #number of trials that have at least one facecard
for _ in range(nTrials):
cards = sample(range(nCards),nDealt)
facecards = false
for c in cards:
s = floor(c/nVals)
v = c- s*nVals
if v > 9:
facecards = true
html('%s$%s$ '%(vals[v],suits[s]),hide=False)
print facecards
if facecards:
nFacecards += 1
print 'Out of %s trials, %s had face cards.'%(nTrials,nFacecards)
︡b262ba94-92b9-47d7-b548-bedb3d3a871c︡{"html":"2$\\color{red}\\diamondsuit$ "}︡{"html":"3$\\clubsuit$ "}︡{"html":"J$\\color{red}\\diamondsuit$ "}︡{"stdout":"True\n"}︡{"html":"9$\\spadesuit$ "}︡{"html":"10$\\color{red}\\heartsuit$ "}︡{"html":"10$\\color{red}\\diamondsuit$ "}︡{"stdout":"False\n"}︡{"html":"Q$\\color{red}\\diamondsuit$ "}︡{"html":"10$\\color{red}\\heartsuit$ "}︡{"html":"3$\\spadesuit$ "}︡{"stdout":"True\n"}︡{"html":"J$\\clubsuit$ "}︡{"html":"6$\\color{red}\\heartsuit$ "}︡{"html":"4$\\color{red}\\heartsuit$ "}︡{"stdout":"True\n"}︡{"html":"J$\\color{red}\\diamondsuit$ "}︡{"html":"2$\\color{red}\\heartsuit$ "}︡{"html":"K$\\color{red}\\heartsuit$ "}︡{"stdout":"True\n"}︡{"html":"4$\\color{red}\\heartsuit$ "}︡{"html":"3$\\spadesuit$ "}︡{"html":"A$\\color{red}\\heartsuit$ "}︡{"stdout":"False\n"}︡{"html":"A$\\color{red}\\heartsuit$ "}︡{"html":"7$\\clubsuit$ "}︡{"html":"7$\\spadesuit$ "}︡{"stdout":"False\n"}︡{"html":"8$\\clubsuit$ "}︡{"html":"3$\\color{red}\\diamondsuit$ "}︡{"html":"6$\\spadesuit$ "}︡{"stdout":"False\n"}︡{"html":"J$\\clubsuit$ "}︡{"html":"9$\\color{red}\\diamondsuit$ "}︡{"html":"9$\\spadesuit$ "}︡{"stdout":"True\n"}︡{"html":"6$\\color{red}\\diamondsuit$ "}︡{"html":"10$\\clubsuit$ "}︡{"html":"4$\\color{red}\\heartsuit$ "}︡{"stdout":"False\n"}︡{"stdout":"Out of 10 trials, 5 had face cards.\n"}︡{"done":true}︡
︠80fef595-ec2f-4c94-ba6a-cd362e61680do︠
#unfair coin
def coin(pHeads):
if random()maxStreakLength:
maxStreakLength = streakLength
else:
start = a
streakLength = 1
return maxStreakLength
@interact
def _(nFlips=[10,20,100,1000],nTrials=[1000,2000]):
streaks = [streak([randint(0,1) for _ in range(nFlips)]) for _ in range(nTrials)]
meanS = float(mean(streaks))
minS = min(streaks)
maxS = max(streaks)
print('%s trials of flipping a coin %s times'%(nTrials,nFlips))
h=histogram(streaks,bins=range(minS-1,maxS+2),align='left',alpha=0.8,color='green')
print('Minimum is %s, maximum is %s, mean is %s'%(minS,maxS,meanS))
show(h)
︡09ae8afc-5c7b-439b-bba4-1d5ee3a1caf7︡{"interact":{"controls":[{"button_classes":null,"buttons":true,"control_type":"selector","default":0,"label":"nFlips","lbls":["10","20","100","1000"],"ncols":null,"nrows":null,"var":"nFlips","width":null},{"button_classes":null,"buttons":true,"control_type":"selector","default":0,"label":"nTrials","lbls":["1000","2000"],"ncols":null,"nrows":null,"var":"nTrials","width":null}],"flicker":false,"id":"cad4ef3e-a6fb-481a-89a7-24f1a9690d26","layout":[[["nFlips",12,null]],[["nTrials",12,null]],[["",12,null]]],"style":"None"}}︡{"done":true}︡
︠af897fc0-1c9d-4177-a432-ad6f716697b9︠
︡950104d8-1606-4bf1-8967-a0829e2cab87︡
︠75bf3f0b-00a1-45a4-bfc7-0268a725c3ec︠
def lottery():
prize = 1
pPrize = .25
pTicket = 0.25
spin = random()
if spin < pPrize:
return prize
else:
if spin