1 USING: help.markup help.syntax math kernel sequences ;
5 { $values { "tuple" "a random number generator" } { "seed" "an integer between 0 and 2^32-1" } }
6 { $description "Seed the random number generator." }
7 { $notes "Not supported on all random number generators." } ;
10 { $values { "tuple" "a random number generator" } { "r" "an integer between 0 and 2^32-1" } }
11 { $description "Generates a random 32-bit unsigned integer." } ;
14 { $values { "n" "an integer" } { "tuple" "a random number generator" } { "byte-array" "a sequence of random bytes" } }
15 { $description "Generates a byte-array of random bytes." } ;
18 { $values { "seq" sequence } { "elt" "a random element" } }
19 { $description "Outputs a random element of the input sequence. Outputs " { $link f } " if the sequence is empty." }
20 { $notes "Since integers are sequences, passing an integer " { $snippet "n" } " outputs an integer in the interval " { $snippet "[0,n)" } "." }
22 { $unchecked-example "USING: random prettyprint ;"
25 { $unchecked-example "USING: random prettyprint ;"
28 "{ heads tails } random ."
33 { $values { "n" "an integer" } { "byte-array" "a random integer" } }
34 { $description "Outputs an integer with n bytes worth of bits." }
36 { $unchecked-example "USING: prettyprint random ;"
38 "B{ 135 50 185 119 240 }"
43 { $values { "numbits" integer } { "r" "a random integer" } }
44 { $description "Outputs an random integer n bits in length." } ;
51 { $description "Returns an integer exactly " { $snippet "numbits" } " in length, with the topmost bit set to one." } ;
55 { $values { "tuple" "a random generator" } { "quot" "a quotation" } }
56 { $description "Calls the quotation with the random generator in a dynamic variable. All random numbers will be generated using this random generator." } ;
58 HELP: with-secure-random
59 { $values { "quot" "a quotation" } }
60 { $description "Calls the quotation with the secure random generator in a dynamic variable. All random numbers will be generated using this random generator." } ;
62 HELP: with-system-random
63 { $values { "quot" "a quotation" } }
64 { $description "Calls the quotation with the system's random generator in a dynamic variable. All random numbers will be generated using this random generator." } ;
66 { with-random with-secure-random with-system-random } related-words
73 { $description "Randomizes a sequence in-place with the Fisher-Yates algorithm and returns the sequence." } ;
77 { "seq" sequence } { "n" integer }
80 { $description "Takes " { $snippet "n" } " samples at random without replacement from a sequence. Throws an error if " { $snippet "n" } " is longer than the sequence." }
82 { $unchecked-example "USING: random prettyprint ; { 1 2 3 } 2 sample ."
91 { $description "Deletes a random number from a sequence using " { $link delete-nth } " and returns the deleted object." } ;
93 ARTICLE: "random-protocol" "Random protocol"
94 "A random number generator must implement one of these two words:"
95 { $subsection random-32* }
96 { $subsection random-bytes* }
97 "Optional, to seed a random number generator:"
98 { $subsection seed-random } ;
100 ARTICLE: "random" "Generating random integers"
101 "The " { $vocab-link "random" } " vocabulary contains a protocol for generating random or pseudorandom numbers."
103 "The “Mersenne Twister” pseudorandom number generator algorithm is the default generator stored in " { $link random-generator } "."
105 "Generate a random object:"
106 { $subsection random }
107 "Combinators to change the random number generator:"
108 { $subsection with-random }
109 { $subsection with-system-random }
110 { $subsection with-secure-random }
112 { $subsection "random-protocol" }
113 "Randomizing a sequence:"
114 { $subsection randomize }
115 "Deleting a random element from a sequence:"
116 { $subsection delete-random }
117 "Random numbers with " { $snippet "n" } " bits:"
118 { $subsection random-bits }
119 { $subsection random-bits* } ;