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

Partial Call Graph (max 5 caller/called nodes):
%3 test_api_randomized api_randomized (test xowiki) xowiki::randomized_indices xowiki::randomized_indices test_api_randomized->xowiki::randomized_indices Class ::xowf::test_item::Question_manager Class ::xowf::test_item::Question_manager (public) Class ::xowf::test_item::Question_manager->xowiki::randomized_indices Class ::xowiki::formfield::ShuffleField Class ::xowiki::formfield::ShuffleField (public) Class ::xowiki::formfield::ShuffleField->xowiki::randomized_indices xowf::test_item::Question_manager instproc question_objs xowf::test_item::Question_manager instproc question_objs (public) xowf::test_item::Question_manager instproc question_objs->xowiki::randomized_indices xowf::test_item::Question_manager instproc shuffled_index xowf::test_item::Question_manager instproc shuffled_index (public) xowf::test_item::Question_manager instproc shuffled_index->xowiki::randomized_indices

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 $shuffled
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: