Version 2 of throw a dice

Updated 2003-12-17 13:42:09

Page by Theo Verelst

Not intended as another tcl or tk gem, but quite seriously, lets make tcl throw dices, and see where we get.

The function to use as most easy way to do this, but unfortunately with predictable results when we know the seed, is of course a subfunction of expr called rand .

To see how pure our dice is, we simply throw it many times and see how often the sides come up, or to make it a bit more interesting, we take a dice which supposedly has a hundred sides it can roll on, and see how many times each side comes up, and draw a graph of the result.

 set to {};
 for {set j 0} {$j < 100} {incr j} {
    set o 0;
    for {set i 0} {$i < 100} {incr i} {
       set o [expr $o+rand()]
    };
    lappend to $o
 }

a hundred times a hundred throws on a row, with the averages stored in a list in variable to. Now lets draw the results to scale, rounded to whole numbers in a little graph:

 canvas .c
 pack .c -expand y -fill both

 for {set i 0} {$i < [llength $to]} {incr i} {
    $mc create line $i 100 $i [expr 100-[lindex $to $i]] -tag g1
 }

http://195.241.128.75/Wiki/throwdice1.jpg