Day 3 Program

Today we wrote our Nspire version of a dice roll simulation. Here’s an example of the function that simulated 1 or more trials of 6 dice.

Define dice(trials)=
Func
 Local i,roll,counts,total_rolls
 total_rolls:=6*trials
 counts:={0,0,0,0,0,0}
 For i,1,total_rolls
 roll:=randInt(1,6)
 counts[roll]:=counts[roll]+1
 EndFor
 Return counts
 EndFunc

And here’s the program that ran multiple trials to determine the experimental probability of rollling 1,2,3,4,5,6 each the same number of times.

Define rollsim(trials,c)=
Prgm
 Local i,counter,roll
 counter:=0
 For i,1,trials
 roll:=dice(c)
 If roll={1,1,1,1,1,1}*c Then
 counter:=counter+1
 EndIf
 EndFor
 Disp "all the same: "&string(counter)
 Disp "EP:"&string(((counter)/(trials))*100.)&"%"
 Return ((counter)/(trials))*1.
EndPrgm

Leave a Reply

Your email address will not be published. Required fields are marked *