1 ! Copyright (C) 2010 Jon Harper.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays combinators.random.private help.markup help.syntax
4 kernel math quotations random sequences ;
8 { $values { "seq" "a sequence of quotations" } }
9 { $description "Calls a random quotation from the given sequence of quotations." } ;
12 { $values { "seq" "a sequence of words" } }
13 { $description "Executes a random word from the given sequence of quotations." } ;
17 { "p" "a number between 0 and 1" } { "true" quotation } { "false" quotation }
19 { $description "Calls the " { $snippet "true" } " quotation with probability " { $snippet "p" }
20 " and the " { $snippet "false" } " quotation with probability (1-" { $snippet "p" } ")." } ;
24 { "assoc" "a sequence of probability/quotations pairs with an optional quotation at the end" }
26 { $description "Calls the different quotations randomly with the given probability. The optional quotation at the end "
27 "will be given a probability so that the sum of the probabilities is one. Therefore, the sum of the probabilities "
28 "must be exactly one when no default quotation is given, or between zero and one when it is given. "
29 "Additionally, all probabilities must be numbers between 0 and 1. "
30 "These rules are enforced during the macro expansion by throwing " { $link bad-probabilities } " "
31 "if they are not respected." }
33 "The following two forms will output 1 with 0.2 probability, 2 with 0.3 probability and 3 with 0.5 probability"
35 "USING: combinators.random prettyprint ;"
44 "USING: combinators.random prettyprint ;"
54 { $see-also casep* } ;
58 { "assoc" "a sequence of probability/word pairs with an optional quotation at the end" }
60 { $description "Calls the different quotations randomly with the given probability. Unlike " { $link casep } ", "
61 "the probabilities are interpreted as conditional probabilities. "
62 "All probabilities must be numbers between 0 and 1. "
63 "The sequence must end with a pair whose probability is one, or a quotation."
64 "These rules are enforced during the macro expansion by throwing " { $link bad-probabilities } " "
65 "if they are not respected." }
67 "The following two forms will output 1 with 0.5 probability, 2 with 0.25 probability and 3 with 0.25 probability"
69 "USING: combinators.random prettyprint ;"
78 "USING: combinators.random prettyprint ;"
91 { "p" "a number between 0 and 1" } { "false" quotation }
93 { $description "Variant of " { $link ifp } " with no " { $snippet "true" } " quotation." } ;
97 { "p" "a number between 0 and 1" } { "true" quotation }
99 { $description "Variant of " { $link ifp } " with no " { $snippet "false" } " quotation." } ;
101 ARTICLE: "combinators.random" "Random combinators"
102 "The " { $vocab-link "combinators.random" } " vocabulary implements simple combinators to easily express random choices"
103 " between multiple code paths."
105 "For all these combinators, the stack effect of the different given quotations or words must be the same."
107 "Variants of if, when and unless:"
120 ABOUT: "combinators.random"