Gustaf Neumann
A xotcl-based enterprise wiki system with multiple object types
2017-08-06
@@ -55,7 +55,7 @@
BSD-Style
2
-
+
Index: openacs-4/packages/xowiki/tcl/xowiki-utility-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/xowiki/tcl/xowiki-utility-procs.tcl,v
diff -u -r1.57.2.19 -r1.57.2.20
--- openacs-4/packages/xowiki/tcl/xowiki-utility-procs.tcl 25 Feb 2021 10:01:36 -0000 1.57.2.19
+++ openacs-4/packages/xowiki/tcl/xowiki-utility-procs.tcl 15 Mar 2021 22:22:18 -0000 1.57.2.20
@@ -55,17 +55,17 @@
}
ad_proc randomized_indices {-seed length} {
- #
- # 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
+ 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
+
} {
# In case, the seed is specified, set the seed to this value to
# achieve e.g. a stable bat random order for a user.
@@ -92,6 +92,20 @@
return $shuffled
}
+
+ ad_proc randomized_index {-seed length} {
+ Produce a single randomized index between 0 and
+ length-1.
+ } {
+ # 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)}
+ }
+ return [expr {int(($length-1) * rand())}]
+ }
+
#
#
# Helper for virus checks
Index: openacs-4/packages/xowiki/tcl/test/api-test-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/xowiki/tcl/test/api-test-procs.tcl,v
diff -u -r1.1.2.7 -r1.1.2.8
--- openacs-4/packages/xowiki/tcl/test/api-test-procs.tcl 16 Feb 2021 17:22:19 -0000 1.1.2.7
+++ openacs-4/packages/xowiki/tcl/test/api-test-procs.tcl 15 Mar 2021 22:22:18 -0000 1.1.2.8
@@ -49,6 +49,39 @@
}
}
+
+aa_register_case \
+ -cats {api smoke production_safe} \
+ -procs {
+ "::xowiki::randomized_index"
+
+ } \
+ api_randomoized {
+
+ Checks randomization functions.
+
+ } {
+ #
+ # single random value, seeded or not
+ #
+ aa_true "randomized index upper" {[::xowiki::randomized_index 10] < 10}
+ aa_true "randomized index lower" {[::xowiki::randomized_index 10] >= 0}
+ aa_equals "randomized index seeded: [::xowiki::randomized_index -seed 123 10]" \
+ [::xowiki::randomized_index -seed 123 10] 1
+ aa_equals "randomized index seeded: [::xowiki::randomized_index -seed 456 10]" \
+ [::xowiki::randomized_index -seed 456 10] 8
+
+ #
+ # randomized indices, seeded or not
+ #
+ aa_equals "randomized indices [::xowiki::randomized_indices -seed 789 5]" \
+ [::xowiki::randomized_indices -seed 789 5] {3 0 4 1 2}
+ aa_equals "randomized indices min " \
+ [::tcl::mathfunc::min {*}[::xowiki::randomized_indices 5]] 0
+ aa_equals "randomized indices min " \
+ [::tcl::mathfunc::max {*}[::xowiki::randomized_indices 5]] 4
+ }
+
#
# Local variables:
# mode: tcl