I am not sure how I would effectively calculate the probability without implementing many many many loops inside of loops. I was able to execute it properly. But there must surely be a better way to write the code. I am not sure if I should include all my code with the loops because it is very long.
code I have so far
import math
arr=[]
total = 0
count =0
# for k in range(1,7):
# for i in range(1,7):
# tupple = (k,i)
# arr.append(tupple)
# count =1
def countConsecutive(array, lengthOfArray):
for i in range(lengthOfArray - 2):
# If consecutive elements are same
if (array[i] == array[i 1]==array[i 2]6):
return True
else:
return False
for a in range(1,7):
for b in range(1,7):
for c in range(1,7):
for d in range(1,7):
for e in range(1,7):
for f in range(1,7):
for g in range(1,7):
for h in range(1,7):
for i in range(1,7):
for j in range(1,7):
for k in range(1,7):
for l in range(1,7):
for m in range(1,7):
for n in range(1,7):
for o in range(1,7):
for p in range(1,7):
for q in range(1,7):
for r in range(1,7):
for s in range(1,7):
for t in range(1,7):
for u in range(1,7):
for v in range(1,7):
for w in range(1,7):
for x in range(1,7):
for y in range(1,7):
for z in range(1,7):
for aa in range(1,7):
for ab in range(1,7):
for ac in range(1,7):
for ad in range(1,7):
for ae in range(1,7):
for ad in range(1,7):
for ae in range(1,7):
for af in range(1,7):
for ag in range(1,7):
for ah in range(1,7):
for ai in range(1,7):
for aj in range(1,7):
for ak in range(1,7):
for al in range(1,7):
for am in range(1,7):
for an in range(1,7):
for ao in range(1,7):
for ap in range(1,7):
for aq in range(1,7):
for ar in range(1,7):
for As in range(1,7):
for at in range(1,7):
for av in range(1,7):
for aw in range(1,7):
for ax in range(1,7):
for ay in range(1,7):
for az in range(1,7):
for ba in range(1,7):
for bb in range(1,7):
for bc in range(1,7):
for bd in range(1,7):
for be in range(1,7):
for bf in range(1,7):
for bg in range(1,7):
for bh in range(1,7):
for bi in range(1,7):
for bj in range(1,7):
for bk in range(1,7):
for bl in range(1,7):
for bm in range(1,7):
for bn in range(1,7):
for bo in range(1,7):
for bp in range(1,7):
for bq in range(1,7):
for br in range(1,7):
for bs in range(1,7):
for bt in range(1,7):
for bu in range(1,7):
for bv in range(1,7):
for bw in range(1,7):
for bx in range(1,7):
for by in range(1,7):
for bz in range(1,7):
for ca in range(1,7):
for cb in range(1,7):
for cc in range(1,7):
for cd in range(1,7):
for ce in range(1,7):
for cf in range(1,7):
for cg in range(1,7):
for ch in range(1,7):
for ci in range(1,7):
for cj in range(1,7):
for ck in range(1,7):
for cl in range(1,7):
for cm in range(1,7):
for cn in range(1,7):
for co in range(1,7):
for cp in range(1,7):
for cq in range(1,7):
for cr in range(1,7):
for cs in range(1,7):
for ct in range(1,7):
for cu in range(1,7):
arr1 = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,q,x,y,z,aa,ab,ac,af,ae,af,ag,ah,ai,aj,ak,al,am,an,ao,ap,aq,ar,As,at,av,aw,ax,ay,az,ba,bb,bc,bd,be,bf,bg,bh,bi,bj,bk,bl,bm,bn,bo,bp,bq,br,bs,bt,bu,bv,bw,bx,by,bz,ca,cb,cc,cd,ce,cf,cg,ch,ci,cj,ck,cl,cm,cn,co,cp,cq,cr,cs,ct,cu]
if countConsecutive(arr1,len(arr1)) == True:
count =1
arr.append(arr1)
print(count/pow(6,100))
CodePudding user response:
You don't need to do any coded virtual experiment for this. That probability is easily calculated.
If you think scenario you take three dice and roll them probability to get three six is (1/6)³ = 1/216. So probability to not have three six is 215/216. If you repeat that hundred times probability to not have all three dice to six even once is (215/216)^100 ≃ 0.6287. So probability to have three six with 100 rolls is 1-0.6287 = 0.3713 ≃ 37.1%.
Scenario where you roll a die hundred times and check if there is three consecutive is almost the same. Slightly smaller because first two rolls it is impossible to have consecutive sixs, so basicly we have 98 tries. 1 - (215/216)^98 ≃ 0.3654 ≃ 36.5%
If you want to write code to test, here is one option.
import random
def try100rolls():
"""
Returns true if there is three consecutive sixs
"""
sixcount = 0
for i in range(100):
if random.randint(1,6) == 6:
sixcount = 1
if sixcount == 3:
return True
else:
sixcount = 0
return False
def test(times):
"""
Do hundred roll test n times. Returns persentage how many times you got three six on row
"""
successcount=0
for i in range(times):
if try100rolls():
successcount = 1
return successcount/times
The more test runs you do the closer result should get to mathematically calculated value. For some reason i got a slightly smaller probabilities when running 100 test runs than right values. It may be related to Python random class or I've calculated something wrong. Or it is just by change.
CodePudding user response:
Here is more simple approach:
import random
sixs = []
counter = 0
for i in range(100):
for x in range(3):
if random.randint(1,6) == 6:
sixs.append(x)
if len(sixs) == 3:
counter = counter 1
sixs = []
print(str(counter) "%")
Import module random
. And create empty list
, in this case sixs[]
and counter
. For
loop will run 100 times and inside for
loop you will have another for
lopp that will run 3 times and check if random.randint(1,6) == 6:
. If generated random
number is 6, append
it to sixs[] list
. when 2nd loop is finished it will check if number 6 is appended 3 times, if it is, it will increase counter
by 1. At the end of the code it will print precentage.
[EDIT]
Just one more line of code is missing, to reset list
back to 0 at the end of itteration. That line is sixs[] = []