factor/extra/random-weighted/random-weighted.factor

16 lines
458 B
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
USING: kernel quotations sequences math math.vectors random ;
IN: random-weighted
: probabilities ( weights -- probabilities )
dup sum [ / ] curry map ;
: layers ( probabilities -- layers )
dup length 1+ [ head ] curry* map 1 tail [ sum ] map ;
: random-weighted ( weights -- elt )
probabilities layers [ 1000 * ] map 1000 random [ > ] curry find drop ;
: random-weighted* ( seq -- elt )
dup [ second ] map swap [ first ] map random-weighted swap nth ;