Version 2 of Throwing Two Dice

Updated 2004-12-30 15:56:50

GWM

After reading throw a dice, I decided to look into throwing 2 dice since many games such as monopoly throw 2 dice on each turn.

Using 2 dice makes life more interesting. The most likely throw is 7 - you can never throw 1 and so on. The following code provides some routines for emulating 1 or 2 dice using the simple rand() function.

 proc throwdice {} {
        set ok 0
        while {!$ok} {
                set sc [expr 1+int(6*rand())]
                if {$sc<7} {set ok 1}
        }
        return $sc
 }
 proc throw2dice {} {
        lappend all  [throwdice]
        lappend all  [throwdice]
        return $all
 }
 proc throw2dicesum {} {
        set thro [ throw2dice]
        return [expr [lindex $thro 0]+[lindex $thro 1]]
 }

 #throwdice emulates a single dice of 6 sides.
 #throw2dice returns a list of the results of 2 throwdice;
 #throw2dicesum returns the sum of two dice.

 proc getfrequency {n} {
  # check how often we arrive at each 'square' from 0 with 2 dice thrown to reach square n at least.
        set i 0
        array set bins {}
        set maxbin $n
        while {$i<=$maxbin} { set bins($i) 0; incr i}        
        set i 0
        while {$i<40000} { ;# do this number of repeats to get statistical sample.
                set j 0
                set pos 0
                while {$pos<$n} { ;# continue until we reach N
                        set thro [throw2dicesum]
                        incr pos $thro
                        if {$pos<=$n} { ;# if > n then dont count any more.
                                incr bins($pos)
                        }
                }
                incr i
        }
        puts "Histogram of frequency of reaching square N from origin."
        set i 0
        while {$i<$maxbin} {
                puts "position\t$i\treached\t$bins($i)\ttimes"
                incr i
        }
        histogram [array get bins] 300 250
        return 0
 }
 global ready
 set ready 0
 proc histogram {slist wid ht} { ;# render a histogram
        global ready
        array set scores $slist
        set nms [lsort -integer [array names scores]]
        catch {destroy .h} {}
        catch {destroy .b} {}
        canvas .h -width $wid -height $ht -bg beige
          pack .h
        set nbars [array size scores] ;#how many histogram bars
        set nax [expr $nbars/10] ;# axis spacing
        set hwid [expr $wid/ $nbars]
        set i 0
        set hmax -9999999 ;# largest y value 
        set hmin  9999999 ;# smallest y value
        while {$i<$nbars} {
                set f $scores([lindex $nms $i])
                set hmax [ expr {$f>$hmax} ? $f : $hmax]
                set hmin [ expr {$f<$hmin} ? $f : $hmin]
                incr i
        }

        if {$hmax>$hmin} {
        set i 0
        set nay 100
        while {$nay<$hmax} {
                set yp [expr $ht-0.75*$nay-20]
                .h create line  0 $yp $wid $yp -fill red
                incr nay 100
        }
        set nax 10
        while {$i<$nbars} {
                set x [expr $hwid*$i]
                set rhs [expr $x+$hwid/2]
                set f [expr $ht-20-.75*$ht*$scores([lindex $nms $i])/$hmax]
                .h create rectangle  $x [expr $ht-20] $rhs $f -fill gray
                if {[expr $i>=$nax]} {
                .h create line  $x [expr $ht-20] $x 0 -fill red
                        incr nax 10
                }
                incr i
                incr x $hwid
        }
        update

        button .b  -text "Next" -command "incr ready" ;# when ready is changed, proceeds with commands
        pack .b
        }
 }

 getfrequency 36

Proc getfrequency finds how often you land on each square 0 to n (eg 0-36) no matter how many throws of the dice this takes.

You will find that the number of times your throw lands on a particular square increases, then decreases, and then oscillates a little.

 Histogram of frequency of reaching square N from origin.
 position        0        reached        0        times
 position        1        reached        0        times
 position        2        reached        1128        times
 position        3        reached        2177        times
 position        4        reached        3385        times
 position        5        reached        4572        times
 position        6        reached        5750        times
 position        7        reached        7411        times
 position        8        reached        6758        times
 position        9        reached        6209        times
 position        10        reached        5826        times
 position        11        reached        5689        times
 position        12        reached        5361        times
 position        13        reached        4964        times
 position        14        reached        5557        times
 position        15        reached        5835        times
 position        16        reached        5880        times
 position        17        reached        5930        times
 position        18        reached        5803        times
 position        19        reached        5784        times
 position        20        reached        5628        times
 position        21        reached        5572        times
 position        22        reached        5710        times
 position        23        reached        5723        times
 position        24        reached        5639        times
 position        25        reached        5782        times
 position        26        reached        5851        times
 position        27        reached        5776        times
 position        28        reached        5593        times
 position        29        reached        5681        times
 position        30        reached        5651        times

I used excel to display the graph, but could use the graph plotter.

Category Mathematics