PageRenderTime 72ms CodeModel.GetById 59ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/unmaintained/random-weighted/random-weighted.factor

http://github.com/abeaumont/factor
Unknown | 20 lines | 13 code | 7 blank | 0 comment | 0 complexity | a12a363c67a99b74e2b41d7de5e4accb MD5 | raw file
 1
 2USING: kernel namespaces arrays quotations sequences assocs combinators
 3       mirrors math math.vectors random macros fry ;
 4
 5IN: random-weighted
 6
 7: probabilities ( weights -- probabilities ) dup sum v/n ;
 8
 9: layers ( probabilities -- layers )
10dup length 1+ [ head ] with map rest [ sum ] map ;
11
12: random-weighted ( weights -- elt )
13probabilities layers [ 1000 * ] map 1000 random [ > ] curry find drop ;
14
15: random-weighted* ( seq -- elt )
16dup [ second ] map swap [ first ] map random-weighted swap nth ;
17
18MACRO: call-random-weighted ( exp -- )
19  [ keys ] [ values <enum> >alist ] bi
20  '[ _ random-weighted _ case ] ;