]> gitweb.factorcode.org Git - factor.git/blob - basis/random/random-docs.factor
Merge branch 'master' of git://github.com/slavapestov/factor
[factor.git] / basis / random / random-docs.factor
1 USING: help.markup help.syntax math kernel sequences ;
2 IN: random
3
4 HELP: seed-random
5 { $values
6     { "tuple" "a random number generator" }
7     { "seed" "a seed specific to the random number generator" }
8     { "tuple'" "a random number generator" }
9 }
10 { $description "Seed the random number generator. Repeatedly seeding the random number generator should provide the same sequence of random numbers." }
11 { $notes "Not supported on all random number generators." } ;
12
13 HELP: random-32*
14 { $values { "tuple" "a random number generator" } { "r" "an integer between 0 and 2^32-1" } }
15 { $description "Generates a random 32-bit unsigned integer." } ;
16
17 HELP: random-bytes*
18 { $values { "n" "an integer" } { "tuple" "a random number generator" } { "byte-array" "a sequence of random bytes" } }
19 { $description "Generates a byte-array of random bytes." } ;
20
21 HELP: random
22 { $values { "obj" object } { "elt" "a random element" } }
23 { $description "Outputs a random element of the input object, or outputs " { $link f } " if the object contains no elements." }
24 { $examples
25     { $unchecked-example "USING: random prettyprint ;"
26         "10 random ."
27         "3" }
28     { $unchecked-example "USING: random prettyprint ;"
29         "SYMBOL: heads"
30         "SYMBOL: tails"
31         "{ heads tails } random ."
32         "heads" }
33 } ;
34
35 HELP: random-32
36 { $values { "n" "a 32-bit random integer" } }
37 { $description "Outputs 32 random bits. This word is more efficient than calling " { $link random } " because no scaling is done on the output." } ;
38
39 HELP: random-bytes
40 { $values { "n" "an integer" } { "byte-array" "a random integer" } }
41 { $description "Outputs an integer with n bytes worth of bits." }
42 { $examples 
43     { $unchecked-example "USING: prettyprint random ;"
44                "5 random-bytes ."
45                "B{ 135 50 185 119 240 }"
46     }
47 } ;
48
49 HELP: random-bits
50 { $values { "numbits" integer } { "r" "a random integer" } }
51 { $description "Outputs an random integer n bits in length." } ;
52
53 HELP: random-bits*
54 { $values
55     { "numbits" integer }
56     { "n" integer }
57 }
58 { $description "Returns an integer exactly " { $snippet "numbits" } " in length, with the topmost bit set to one." } ;
59
60
61 HELP: with-random
62 { $values { "tuple" "a random generator" } { "quot" "a quotation" } }
63 { $description "Calls the quotation with the random generator in a dynamic variable.  All random numbers will be generated using this random generator." } ;
64
65 HELP: with-secure-random
66 { $values { "quot" "a quotation" } }
67 { $description "Calls the quotation with the secure random generator in a dynamic variable.  All random numbers will be generated using this random generator." } ;
68
69 HELP: with-system-random
70 { $values { "quot" "a quotation" } }
71 { $description "Calls the quotation with the system's random generator in a dynamic variable.  All random numbers will be generated using this random generator." } ;
72
73 { with-random with-secure-random with-system-random } related-words
74
75 HELP: randomize
76 { $values
77      { "seq" sequence }
78      { "randomized" sequence }
79 }
80 { $description "Randomizes a sequence in-place with the Fisher-Yates algorithm and returns the sequence." } ;
81
82 HELP: sample
83 { $values
84     { "seq" sequence } { "n" integer }
85     { "seq'" sequence }
86 }
87 { $description "Takes " { $snippet "n" } " samples at random without replacement from a sequence. Throws an error if " { $snippet "n" } " is longer than the sequence." }
88 { $examples
89     { $unchecked-example "USING: random prettyprint ;"
90     "{ 1 2 3 } 2 sample ."
91     "{ 3 2 }"
92     }
93 } ;
94
95 HELP: delete-random
96 { $values
97      { "seq" sequence }
98      { "elt" object } }
99 { $description "Deletes a random number from a sequence using " { $link remove-nth! } " and returns the deleted object." } ;
100
101 ARTICLE: "random-protocol" "Random protocol"
102 "A random number generator must implement one of these two words:"
103 { $subsections
104     random-32*
105     random-bytes*
106 }
107 "Optional, to seed a random number generator:"
108 { $subsections seed-random } ;
109
110 ARTICLE: "random" "Generating random integers"
111 "The " { $vocab-link "random" } " vocabulary contains a protocol for generating random or pseudorandom numbers."
112 $nl
113 "The “Mersenne Twister” pseudorandom number generator algorithm is the default generator stored in " { $link random-generator } "."
114 $nl
115 "Generate a random object:"
116 { $subsections random }
117 "Efficient 32-bit random numbers:"
118 { $subsections random-32 }
119 "Combinators to change the random number generator:"
120 { $subsections
121     with-random
122     with-system-random
123     with-secure-random
124 }
125 "Implementation:"
126 { $subsections "random-protocol" }
127 "Randomizing a sequence:"
128 { $subsections randomize }
129 "Sampling a sequences:"
130 { $subsections sample }
131 "Deleting a random element from a sequence:"
132 { $subsections delete-random }
133 "Random numbers with " { $snippet "n" } " bits:"
134 { $subsections
135     random-bits
136     random-bits*
137 } ;
138
139 ABOUT: "random"