xowiki::randomized_indices (public)
xowiki::randomized_indices [ -seed seed ] length
Defined in packages/xowiki/tcl/xowiki-utility-procs.tcl
Produce a list of "length" random numbers between 0 and length-1. Measure quality of randomization:
time {lappend _ [xowiki::randomized_indices -seed [clock microseconds] 3]} 1000 foreach t $_ { lassign $t a b c; dict incr stats "a $a"; dict incr stats "b $b"; dict incr stats "c $c" } set stats
- Switches:
- -seed (optional)
- Parameters:
- length (required)
- Partial Call Graph (max 5 caller/called nodes):
- Testcases:
- api_randomized
Source code: # In case, the seed is specified, set the seed to this value to # achieve e.g. a stable bat random order for a user. # if {[info exists seed]} { expr {srand($seed)} } # # Produce shuffled indices between 0 and length-1. # set indices {} for {set i 0} {$i < $length} {incr i} { lappend indices $i } set shuffled {} incr length for {} {$length > 1} {incr length -1} { set r [expr {rand()}] set i [expr {int(($length-1) * $r)}] #ns_log notice "[list expr int([expr ($length-1)] * $r)] -> [expr {($length-1) * $r}] -> $i" lappend shuffled [lindex $indices $i] set indices [lreplace $indices $i $i] } return $shuffledXQL Not present: Generic, PostgreSQL, Oracle