Posted to tcl by sebres at Fri Dec 13 14:43:08 GMT 2019view raw
- apply {{} {
- set max 4
- array set weights {0 1 1 0 2 5 3 9 4 7}
- set n 0; while {$n <= $max} { set rtmw($n) 0; incr n }
- proc genrnd {} {
- upvar max max weights weights rtmw rtmw
- while 1 {
- set n [expr { int(rand() * ($max+1)) }]
- if { $weights($n) && [incr rtmw($n)] >= 10-$weights($n) } {
- set rtmw($n) 0
- break
- }
- }
- return $n
- }
- timerate { incr num([genrnd]) } 1000
- puts [lsort -integer -stride 2 -index 1 -decr [array get num]]
- }}
- # results:
- 3 261842 4 87174 2 52208 0 28929