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:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
proc test {} {
proc randlist {a b} { return [expr {int(rand()*2) ? -1 : 1}] }
proc repeat { what {times 1}} {
for {set a 0} {$a < $times} {incr a} {
uplevel 1 $what
}
}
puts "num prob. to be found in list at that place:"
puts "x 1 2 3 4 5 6 7 8 9 0"
for {set i 0} { $i <= 9 } {incr i} {
set prob [list $i]
for {set j 0} { $j <= 9} { incr j} {
set k 0
repeat {if {[lsearch [lsort -command randlist [list 1 2 3 4 5 6 7 8 9 0]] $i] == $j} { incr k }} 1000
lappend prob [expr {($k*100)/1000}]
}
puts [join $prob " "]
}
}
|
Running this gives us:
01
02
03
04
05
06
07
08
09
10
11
12
|
num prob. to be found in list at that place:
x 1 2 3 4 5 6 7 8 9 0
0 24 25 19 12 6 5 1 1 1 1
1 5 6 7 8 11 11 10 12 13 11
2 5 5 8 9 10 13 11 11 10 11
3 6 4 7 9 9 11 12 11 13 11
4 5 6 8 7 10 11 11 11 10 13
5 7 6 6 9 11 10 10 11 11 12
6 5 5 8 9 12 10 10 11 12 12
7 5 7 8 7 10 11 10 12 12 13
8 6 5 8 7 12 11 10 12 12 13
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 |