Posted to tcl by sebres at Fri Dec 13 14:43:08 GMT 2019view raw

  1. apply {{} {
  2. set max 4
  3. array set weights {0 1 1 0 2 5 3 9 4 7}
  4. set n 0; while {$n <= $max} { set rtmw($n) 0; incr n }
  5. proc genrnd {} {
  6. upvar max max weights weights rtmw rtmw
  7. while 1 {
  8. set n [expr { int(rand() * ($max+1)) }]
  9. if { $weights($n) && [incr rtmw($n)] >= 10-$weights($n) } {
  10. set rtmw($n) 0
  11. break
  12. }
  13. }
  14. return $n
  15. }
  16. timerate { incr num([genrnd]) } 1000
  17. puts [lsort -integer -stride 2 -index 1 -decr [array get num]]
  18. }}
  19.  
  20. # results:
  21. 3 261842 4 87174 2 52208 0 28929
  22.