Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
20 lines (15 sloc) 451 Bytes
#lang scheme/base
(require
scheme/dict)
(provide random-multinomial)
;; random-multinomial : multinomial [random-source] -> any
(define (random-multinomial m [random-source random])
(define choice (random-source))
;;(printf "random-multinomial: ~a\n" m)
(let/ec esc
(for/fold ([sum 0.0])
(([v p] (in-dict m)))
(let ([new-sum (+ sum p)])
(if (<= choice new-sum)
(esc v)
new-sum)))))