Imho this doesn't really randomize a list. I wrote this proc to check how big the probability is to find a entry at a certain position of the list:
| 1 | proc test {} { | | 2 | proc randlist {a b} { return [expr {int(rand()*2) ? -1 : 1}] } | | 3 | proc repeat { what {times 1}} { | | 4 | for {set a 0} {$a < $times} {incr a} { | | 5 | uplevel 1 $what | | 6 | } | | 7 | } | | 8 | puts "num prob. to be found in list at that place:" | | 9 | puts "x 1 2 3 4 5 6 7 8 9 0" | | 10 | for {set i 0} { $i <= 9 } {incr i} { | | 11 | set prob [list $i] | | 12 | for {set j 0} { $j <= 9} { incr j} { | | 13 | set k 0 | | 14 | repeat {if {[lsearch [lsort -command randlist [list 1 2 3 4 5 6 7 8 9 0]] $i] == $j} { incr k }} 1000 | | 15 | lappend prob [expr {($k*100)/1000}] | | 16 | } | | 17 | puts [join $prob " "] | | 18 | } | | 19 | } |
Running this gives us:
| 1 | num prob. to be found in list at that place: | | 2 | x 1 2 3 4 5 6 7 8 9 0 | | 3 | 0 24 25 19 12 6 5 1 1 1 1 | | 4 | 1 5 6 7 8 11 11 10 12 13 11 | | 5 | 2 5 5 8 9 10 13 11 11 10 11 | | 6 | 3 6 4 7 9 9 11 12 11 13 11 | | 7 | 4 5 6 8 7 10 11 11 11 10 13 | | 8 | 5 7 6 6 9 11 10 10 11 11 12 | | 9 | 6 5 5 8 9 12 10 10 11 12 12 | | 10 | 7 5 7 8 7 10 11 10 12 12 13 | | 11 | 8 6 5 8 7 12 11 10 12 12 13 | | 12 | 9 22 23 18 12 8 3 3 1 0 0 |
This means that the number 0 (last entry in list) has a probability to be in the first position of the list after the "randomisation" of 24%. On a perfect shuffle this should be 10% (100/(entries in list))
This "gap" gets smaller with more entries ( with 15 entries its barely noticable) but worse with less (with 5 entries the last entry has a prob of 50% to be first after the shuffle)
Or am i calculating it wrong?
for a more in dept discussion about shuffleling a list: http://wiki.tcl.tk/941 |