1 USING: help.markup help.syntax math kernel sequences ;
6 { "tuple" "a random number generator" }
7 { "seed" "a seed specific to the random number generator" }
8 { "tuple'" "a random number generator" }
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." } ;
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." } ;
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." } ;
22 { $values { "seq" sequence } { "elt" "a random element" } }
23 { $description "Outputs a random element of the input sequence. Outputs " { $link f } " if the sequence is empty." }
24 { $notes "Since integers are sequences, passing an integer " { $snippet "n" } " outputs an integer in the interval " { $snippet "[0,n)" } "." }
26 { $unchecked-example "USING: random prettyprint ;"
29 { $unchecked-example "USING: random prettyprint ;"
32 "{ heads tails } random ."
37 { $values { "n" "a 32-bit random integer" } }
38 { $description "Outputs 32 random bits. This word is more efficient than calling " { $link random } " because no scaling is done on the output." } ;
41 { $values { "n" "an integer" } { "byte-array" "a random integer" } }
42 { $description "Outputs an integer with n bytes worth of bits." }
44 { $unchecked-example "USING: prettyprint random ;"
46 "B{ 135 50 185 119 240 }"
51 { $values { "numbits" integer } { "r" "a random integer" } }
52 { $description "Outputs an random integer n bits in length." } ;
59 { $description "Returns an integer exactly " { $snippet "numbits" } " in length, with the topmost bit set to one." } ;
63 { $values { "tuple" "a random generator" } { "quot" "a quotation" } }
64 { $description "Calls the quotation with the random generator in a dynamic variable. All random numbers will be generated using this random generator." } ;
66 HELP: with-secure-random
67 { $values { "quot" "a quotation" } }
68 { $description "Calls the quotation with the secure random generator in a dynamic variable. All random numbers will be generated using this random generator." } ;
70 HELP: with-system-random
71 { $values { "quot" "a quotation" } }
72 { $description "Calls the quotation with the system's random generator in a dynamic variable. All random numbers will be generated using this random generator." } ;
74 { with-random with-secure-random with-system-random } related-words
81 { $description "Randomizes a sequence in-place with the Fisher-Yates algorithm and returns the sequence." } ;
85 { "seq" sequence } { "n" integer }
88 { $description "Takes " { $snippet "n" } " samples at random without replacement from a sequence. Throws an error if " { $snippet "n" } " is longer than the sequence." }
90 { $unchecked-example "USING: random prettyprint ; { 1 2 3 } 2 sample ."
99 { $description "Deletes a random number from a sequence using " { $link delete-nth } " and returns the deleted object." } ;
101 ARTICLE: "random-protocol" "Random protocol"
102 "A random number generator must implement one of these two words:"
103 { $subsection random-32* }
104 { $subsection random-bytes* }
105 "Optional, to seed a random number generator:"
106 { $subsection seed-random } ;
108 ARTICLE: "random" "Generating random integers"
109 "The " { $vocab-link "random" } " vocabulary contains a protocol for generating random or pseudorandom numbers."
111 "The “Mersenne Twister” pseudorandom number generator algorithm is the default generator stored in " { $link random-generator } "."
113 "Generate a random object:"
114 { $subsection random }
115 "Efficient 32-bit random numbers:"
116 { $subsection random-32 }
117 "Combinators to change the random number generator:"
118 { $subsection with-random }
119 { $subsection with-system-random }
120 { $subsection with-secure-random }
122 { $subsection "random-protocol" }
123 "Randomizing a sequence:"
124 { $subsection randomize }
125 "Sampling a sequences:"
126 { $subsection sample }
127 "Deleting a random element from a sequence:"
128 { $subsection delete-random }
129 "Random numbers with " { $snippet "n" } " bits:"
130 { $subsection random-bits }
131 { $subsection random-bits* } ;