Terrapin Resources

Dice Simulation

by Stan Munson

Adjust the slider for the number of rolls you want to simulate.
Note that you can also use the left and right arrow keys to change this value.
Then click the Roll count button.

The DICE chart shows each die separately. One die is tabulated in the columns; the other die is tabulated in the rows. This lets you see, for example, how many times you rolled a 5/4 combination compared to a 4/5 combination.

The DICE chart tabulates all die rolls. The total here is always twice your request since the simulation is for pairs of dice. The ROLL chart tabluates the sum of each dice roll.

The percentages for the DICE chart rows and columns and the percentages for the DIE rows should be very close when a large number of rolls is requested. The percentages for the ROLL chart are a starting points for investigating the game of craps.

Note that you can turn off the dice roll animation for large numbers.

Dice_Simulation.lgo

TO WORKSHEET :SNAME :ROWS :COLS :ROW.TITLES :COL.TITLES :SIZE :SXY
	(LOCAL "BNAME "BX "BY "BWIDTH "BHEIGHT "ROW "COL)
	MAKE "BWIDTH FIRST :SIZE
	MAKE "BHEIGHT LAST :SIZE
	MAKE "BX (FIRST :SXY) + (:BWIDTH / 2)
	MAKE "BY (LAST :SXY) - (:BHEIGHT / 2)
	MAKE "ROW 0
	WHILE [.LE :ROW :ROWS] [
		MAKE "COL 0
		WHILE [.LE :COL :COLS] [
			MAKE "BNAME (WORD :SNAME ".R :ROW "C :COL)
			DECLARE "EDITBOX :BNAME
			PPROP :BNAME "POSITION LIST :BX :BY
			PPROP :BNAME "SIZE :SIZE
			MAKE "COL :COL + 1
			MAKE "BX :BX + (:BWIDTH - 1)
		]
		MAKE "BX (FIRST :SXY) + (:BWIDTH / 2)
		MAKE "ROW :ROW + 1
		MAKE "BY :BY - (:BHEIGHT - 1)
	]

; Fill in column headings with bold font

	MAKE "ROW 0
	MAKE "COL 1
	WHILE [.LE :COL :COLS] [
		MAKE "BNAME (WORD :SNAME ".R :ROW "C :COL)
		PPROP :BNAME "TEXT ITEM :COL :COL.TITLES
		PPROP :BNAME "FONT SENTENCE BUTLAST GPROP :BNAME "FONT 1
		MAKE "COL :COL + 1
	]

; Fill in row headings with bold font

	MAKE "ROW 1
	MAKE "COL 0
	WHILE [.LE :ROW :ROWS] [
		MAKE "BNAME (WORD :SNAME ".R :ROW "C :COL)
		PPROP :BNAME "TEXT ITEM :ROW :ROW.TITLES
		PPROP :BNAME "FONT SENTENCE BUTLAST GPROP :BNAME "FONT 1
		MAKE "ROW :ROW + 1
	]

; Set row 0 column 0 font to bold

	MAKE "BNAME (WORD :SNAME ".R0C0)
	PPROP :BNAME "FONT SENTENCE BUTLAST GPROP :BNAME "FONT 1
	PPROP :BNAME "TEXT :SNAME

; Set the properties of the SNAME

	PPROPS :SNAME (SENTENCE "MAXROW :ROWS "MAXCOL :COLS)
END

TO GET.CELL :GRIDNAME :ROW :COL
	OUTPUT GPROP (WORD :GRIDNAME ".R :ROW "C :COL) "TEXT
END

TO SET.CELL :GRIDNAME :ROW :COL :VALUE
	PPROP (WORD :GRIDNAME ".R :ROW "C :COL) "TEXT :VALUE
END

TO INC.CELL :GRIDNAME :ROW :COL [:INC 1] 3
	LOCAL "TEMP
	MAKE "TEMP GET.CELL :GRIDNAME :ROW :COL
	IF EMPTY? :TEMP MAKE "TEMP 0
	IF NOT NUMBER? :TEMP [
			(THROW "INC.CELL (LIST "|Cell| (WORD :GRIDNAME ".R :ROW "C :COL) "|is not numeric.|))
	]
	MAKE "TEMP :TEMP + :INC
	SET.CELL :GRIDNAME :ROW :COL :TEMP
END

TO SUM.ROW :GRIDNAME :ROW
	(LOCAL "TOTAL "COL "MAXCOL)
	MAKE "MAXCOL GPROP :GRIDNAME "MAXCOL
	MAKE "TOTAL 0
	MAKE "COL 1
	WHILE [.LE :COL :MAXCOL] [
		MAKE "TEMP GET.CELL :GRIDNAME :ROW :COL
		IF NUMBER? :TEMP [MAKE "TOTAL :TOTAL + :TEMP]
		MAKE "COL :COL + 1
	]
	OUTPUT :TOTAL
END

TO SUM.COL :GRIDNAME :COL
	(LOCAL "TOTAL "ROW "MAXROW)
	MAKE "MAXROW GPROP :GRIDNAME "MAXROW
	MAKE "TOTAL 0
	MAKE "ROW 1
	WHILE [.LE :ROW :MAXROW] [
		MAKE "TEMP GET.CELL :GRIDNAME :ROW :COL
		IF NUMBER? :TEMP [MAKE "TOTAL :TOTAL + :TEMP]
		MAKE "ROW :ROW + 1
	]
	OUTPUT :TOTAL
END

TO SUM.CELLS :GRIDNAME :UPPER.ROW :LEFT.COL :LOWER.ROW :RIGHT.COL
	(LOCAL "ROW "COL "ROW.BEGIN "ROW.END "COL.BEGIN "COL.END "TOTAL)
	MAKE "ROW.BEGIN :UPPER.ROW
	MAKE "COL.BEGIN :LEFT.COL
	MAKE "ROW.END :LOWER.ROW
	MAKE "COL.END :RIGHT.COL
	MAKE "TOTAL 0
	MAKE "ROW :ROW.BEGIN
	WHILE [.LE :ROW :ROW.END] [
		MAKE "COL :COL.BEGIN
		WHILE [.LE :COL :COL.END] [
			MAKE "TEMP GET.CELL :GRIDNAME :ROW :COL
			IF NUMBER? :TEMP [MAKE "TOTAL :TOTAL + :TEMP]
			MAKE "COL :COL + 1
		]
		MAKE "ROW :ROW + 1
	]
	OUTPUT :TOTAL
END

TO INIT.GRID :GRIDNAME :VALUE
	(LOCAL "ROW "COL "MAXROW "MAXCOL)
	MAKE "MAXROW GPROP :GRIDNAME "MAXROW
	MAKE "MAXCOL GPROP :GRIDNAME "MAXCOL
	MAKE "ROW 1
	WHILE [.LE :ROW :MAXROW] [
		MAKE "COL 1
		WHILE [.LE :COL :MAXCOL] [
			SET.CELL :GRIDNAME :ROW :COL :VALUE
			MAKE "COL :COL + 1
		]
		MAKE "ROW :ROW + 1
	]
END

TO SIMULATE :THROWS
	FOREACH [DICE ROLL DIE] [INIT.GRID "? 0]
	FOR "ROLLS 1 :THROWS [TABULATE]
END

TO TABULATE
	(LOCAL "ROLL "DIE1 "DIE2)
	MAKE "ROLL ROLL.DICE
	MAKE "DIE1 FIRST :ROLL
	MAKE "DIE2 LAST :ROLL

; Change the image of the dice to simulate the actual roll

	IF GPROP "ANIMATION.CHECK "STATE [
		ASK "DIE.1 [LOADSHAPE (WORD "~HOME/TOOLBOX/DICE/DICE PICK THING WORD "D :DIE1 ".PNG)]
		ASK "DIE.2 [LOADSHAPE (WORD "~HOME/TOOLBOX/DICE/DICE PICK THING WORD "D :DIE2 ".PNG)]
	]

	INC.CELL "DICE :DIE1 :DIE2
	INC.CELL "DICE :DIE1 7
	INC.CELL "DICE 7 :DIE2
	INC.CELL "DICE 7 7
	INC.CELL "ROLL 1 (:DIE1 + :DIE2 - 1)
	INC.CELL "ROLL 1 12
	INC.CELL "DIE :DIE1 1
	INC.CELL "DIE 7 1
	INC.CELL "DIE :DIE2 1
	INC.CELL "DIE 7 1
	FOR "I 1 6 [
		SET.CELL "DICE :I 8 PRODUCT 100 QUOTIENT (GET.CELL "DICE :I 7) :ROLLS
		SET.CELL "DICE 8 :I PRODUCT 100 QUOTIENT (GET.CELL "DICE 7 :I) :ROLLS
		SET.CELL "DIE :I 2 PRODUCT 100 QUOTIENT (GET.CELL "DIE :I 1) (2 * :ROLLS)
	]
	FOR "I 2 12 [
		SET.CELL "ROLL 2 (:I - 1) PRODUCT 100 QUOTIENT (GET.CELL "ROLL 1 (:I - 1)) :ROLLS
	]
END

TO ROLL.DICE
	OUTPUT LIST RANDOM 6 RANDOM 6
END

TO ABOUT
	(LOCAL "LF "PP "SAMPLE.TEXT "P1 "P2 "P3 "P4 "P5 "P6 "P7 "P8 "P9 "P10)
	MAKE "LF CHAR 10
	MAKE "PP WORD :LF :LF
	MAKE "P1 "|Adjust the slider for the number of rolls you want to simulate and click |
	MAKE "P2 "|the roll count button. |
	MAKE "P3 "|The DICE chart shows each die separately. One die is tabulated in the columns; |
	MAKE "P4 "|the other die is tabulated in the rows. This lets you see, for example, how many |
	MAKE "P5 "|times you rolled a 5/4 combination compared to a 4/5 combination. |
	MAKE "P6 "|The DIE chart tabulates all die rolls. The total here is always twice your request since |
	MAKE "P7 "|the simulation is for pairs of dice. The ROLL chart tabulates the sum of each dice roll. |
	MAKE "P8 "|The percentages for DICE chart rows and columns and the percentages for the DIE rows |
	MAKE "P9 "|should be very close when a large number of rolls is requested. |
	MAKE "P10 "|The percentages for the ROLL chart are a starting point for investigating the game of craps.|

	MAKE "SAMPLE.TEXT (WORD :P1 :P2 :PP :P3 :P4 :P5 :PP :P6 :P7 :PP :P8 :P9 :P10)
	IGNORE ALERT :SAMPLE.TEXT
END

TO MAIN
	PRINT `Move the slider for the number of times to roll the dice.`
	PRINT `Then click the button to roll that many times.`
	PRINT `Turn off animation for large numbers.`
	CLEARSCREEN DRAW
	PPROP "LOGO.ENV "LAYOUT "SMALL
	SPLITSCREEN

; "ROLL.ROWHEADING "ROLL.COLHEADING
	MAKE "DICE.ROWHEADING [1 2 3 4 5 6 |Total| | % |]
	MAKE "DICE.COLHEADING [1 2 3 4 5 6 |Total| | % |]
	MAKE "ROLL.ROWHEADING [|Total| | % |]
	MAKE "ROLL.COLHEADING [2 3 4 5 6 7 8 9 10 11 12 |Rolls|]
	MAKE "DIE.ROWHEADING [1 2 3 4 5 6 |Total|]
	MAKE "DIE.COLHEADING [|Total| | % |]
	WORKSHEET "DICE 8 8 :DICE.ROWHEADING :DICE.COLHEADING [50 20] [-300 130]
	WORKSHEET "ROLL 2 12 :ROLL.ROWHEADING :ROLL.COLHEADING [40 20] [-300 -50]
	WORKSHEET "DIE 7 2 :DIE.ROWHEADING :DIE.COLHEADING [50 20] [200 130]
	INIT.GRID "DICE 0
	INIT.GRID "ROLL 0
	INIT.GRID "DIE 0

; Set up button and slider

	DECLARE "SLIDER "ROLL.SLIDER
	PPROPS "ROLL.SLIDER [POSITION [-140 -140] MINIMUM 1 MAXIMUM 5000 VALUE 77 TOOLTIP |Set the number of rolls|]
	PPROP "ROLL.SLIDER "RUN [PPROP "ROLL.BUTTON "TEXT (WORD "|Roll count: | GPROP "ROLL.SLIDER "VALUE)]
	DECLARE "BUTTON "ROLL.BUTTON
	PPROPS "ROLL.BUTTON [POSITION [-30 -140] TEXT "|Roll count: 77| RUN [SIMULATE GPROP "ROLL.SLIDER "VALUE] TOOLTIP |Click to begin|]

	DECLARE "CHECKBOX "ANIMATION.CHECK
	PPROPS "ANIMATION.CHECK [POSITION [250 -140] STATE TRUE TEXT "|Check to animate dice| TOOLTIP |Check to see the dice images change|]

; The dice images all show 3 numbers but the value of the die is the first digit.

	MAKE "D1 [123 135 142 154]
	MAKE "D2 [214 231 246 263]
	MAKE "D3 [312 326 351 365]
	MAKE "D4 [415 421 456 462]
	MAKE "D5 [513 536 541 564]
	MAKE "D6 [624 632 645 653]

	DECLARE "BITMAP "DIE.1
	DECLARE "BITMAP "DIE.2
	TELL "DIE.1 SETXY [250 -70]
	TELL "DIE.2 SETXY [320 -70]

	SIMULATE 77	; just to get things started
END

MAIN

Procedure MAIN
Description An analysis of rolls of pairs of dice.
Level Advanced
Tags Math, Random