Version 3 of where random numbers flop

Updated 2007-02-15 16:40:17 by suchenwi

proc roll {} {

        set seed [expr [clock clicks] + [pid] + [clock seconds]]
        set seed [expr $seed - [expr $seed * 2]]
        #set seed [expr sqrt($seed)]
        expr (srand ($seed)) 
        set seed 0
        set number [expr (rand() * 6)]
        return [set number [expr round($number)]]
 }

 set ::a 0
 set ::1 0
 set ::2 0
 set ::3 0
 set ::4 0
 set ::5 0
 set ::6 0 

 proc statistics {} {
        set a 0
        while {$a != 100000} {
                set roll [roll]

                while {$roll == 0} {
                        set roll [roll]
                }

                foreach value {1 2 3 4 5 6} {
                        if {$value == $roll} {
                                incr ::$value
                        }
                }
                incr a
        } 
        set a 0

        puts ">1= $::1" 
        puts ">2= $::2" 
        puts ">3= $::3" 
        puts ">4= $::4" 
        puts ">5= $::5" 
        puts ">6= $::6"

        set ::1 0
        set ::2 0
        set ::3 0
        set ::4 0
        set ::5 0
        set ::6 0
 }

This snipped of code should produce random numbers in the range of 1-6, but if you apply a large enough sample, you notice that the output becomes rather predictable. The generator tends to favor 3 and 4 in this instance, and favors 1 rather than 6. This might have something to do with the way round() handles its math, but given the source code above, theres a rather large degree of predictability going on. If I were a gambler Id have my money on 3 everytime.

 100000
 >1= 17471
 >2= 6785
 >3= 25872
 >4= 24633
 >5= 19663
 >6= 5576

 100000
 >1= 17226
 >2= 7011
 >3= 25862
 >4= 25001
 >5= 19008
 >6= 5892

 100000
 >1= 17361
 >2= 7183
 >3= 24947
 >4= 24568
 >5= 19996
 >6= 5945

originally created by Davou


AM (15 february 2007) Why do you seed and reseed rand() every time? That is not the proper way to use rand(). I think much of the non-randomness you are experiencing could be attributed to that (the seeds tend to be correlated!)