2008-10-04 12:44:12 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								USING:  help.markup  help.syntax  math  kernel  sequences  ;
 
							 
						 
					
						
							
								
									
										
										
										
											2008-03-29 00:20:33 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								IN:  random 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								HELP:  seed-random 
							 
						 
					
						
							
								
									
										
										
										
											2009-09-30 04:22:11 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $values
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    { "tuple"  "a random number generator"  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    { "seed"  "a seed specific to the random number generator"  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    { "tuple'"  "a random number generator"  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Seed the random number generator. Repeatedly seeding the random number generator should provide the same sequence of random numbers."  }
							 
						 
					
						
							
								
									
										
										
										
											2008-03-29 00:20:33 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								{ $notes "Not supported on all random number generators."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2009-09-30 16:56:02 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								HELP:  random-32* 
							 
						 
					
						
							
								
									
										
										
										
											2008-03-29 00:20:33 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								{ $values { "tuple"  "a random number generator"  } { "r"  "an integer between 0 and 2^32-1"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Generates a random 32-bit unsigned integer."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								HELP:  random-bytes* 
							 
						 
					
						
							
								
									
										
										
										
											2008-03-29 21:12:22 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								{ $values { "n"  "an integer"  } { "tuple"  "a random number generator"  } { "byte-array"  "a sequence of random bytes"  } }
							 
						 
					
						
							
								
									
										
										
										
											2008-03-29 00:20:33 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Generates a byte-array of random bytes."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								HELP:  random 
							 
						 
					
						
							
								
									
										
										
										
											2010-01-14 13:45:30 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $values { "obj"  object  } { "elt"  "a random element"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Outputs a random element of the input object, or outputs "  { $link f  } " if the object contains no elements."  }
							 
						 
					
						
							
								
									
										
										
										
											2008-10-05 16:14:05 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $examples
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    { $unchecked-example "USING: random prettyprint ;" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        "10 random ." 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        "3"  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    { $unchecked-example "USING: random prettyprint ;" 
							 
						 
					
						
							
								
									
										
										
										
											2008-10-05 23:08:13 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								        "SYMBOL: heads" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        "SYMBOL: tails" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        "{ heads tails } random ." 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        "heads"  }
							 
						 
					
						
							
								
									
										
										
										
											2008-10-05 16:14:05 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								} ;
 
							 
						 
					
						
							
								
									
										
										
										
											2008-03-29 00:20:33 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2009-09-30 16:56:02 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								HELP:  random-32 
							 
						 
					
						
							
								
									
										
										
										
											2009-09-30 19:52:01 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $values { "n"  "a 32-bit random integer"  } }
							 
						 
					
						
							
								
									
										
										
										
											2009-09-30 16:56:02 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $description "Outputs 32 random bits. This word is more efficient than calling "  { $link random } " because no scaling is done on the output."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-03-29 00:20:33 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								HELP:  random-bytes 
							 
						 
					
						
							
								
									
										
										
										
											2008-03-29 21:12:22 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								{ $values { "n"  "an integer"  } { "byte-array"  "a random integer"  } }
							 
						 
					
						
							
								
									
										
										
										
											2008-09-09 16:39:40 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $description "Outputs an integer with n bytes worth of bits."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $examples 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    { $unchecked-example "USING: prettyprint random ;" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								               "5 random-bytes ." 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								               "B{ 135 50 185 119 240 }" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} ;
 
							 
						 
					
						
							
								
									
										
										
										
											2008-03-29 00:20:33 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								HELP:  random-bits 
							 
						 
					
						
							
								
									
										
										
										
											2009-05-10 14:39:08 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $values { "numbits"  integer  } { "r"  "a random integer"  } }
							 
						 
					
						
							
								
									
										
										
										
											2008-03-29 00:20:33 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Outputs an random integer n bits in length."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2009-05-10 14:39:08 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								HELP:  random-bits* 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $values
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    { "numbits"  integer  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    { "n"  integer  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Returns an integer exactly "  { $snippet "numbits"  } " in length, with the topmost bit set to one."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-03-29 00:20:33 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								HELP:  with-random 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $values { "tuple"  "a random generator"  } { "quot"  "a quotation"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Calls the quotation with the random generator in a dynamic variable.  All random numbers will be generated using this random generator."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								HELP:  with-secure-random 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $values { "quot"  "a quotation"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Calls the quotation with the secure random generator in a dynamic variable.  All random numbers will be generated using this random generator."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-10-04 12:44:12 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								HELP:  with-system-random 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $values { "quot"  "a quotation"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Calls the quotation with the system's random generator in a dynamic variable.  All random numbers will be generated using this random generator."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ with-random with-secure-random with-system-random } related-words
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2009-02-14 15:25:48 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								HELP:  randomize 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $values
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								     { "seq"  sequence  }
							 
						 
					
						
							
								
									
										
										
										
											2010-02-18 18:31:52 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								     { "randomized"  sequence  }
							 
						 
					
						
							
								
									
										
										
										
											2009-02-14 15:25:48 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Randomizes a sequence in-place with the Fisher-Yates algorithm and returns the sequence."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2009-09-23 13:04:06 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								HELP:  sample 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $values
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    { "seq"  sequence  } { "n"  integer  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    { "seq'"  sequence  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $description "Takes "  { $snippet "n"  } " samples at random without replacement from a sequence. Throws an error if "  { $snippet "n"  } " is longer than the sequence."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $examples
							 
						 
					
						
							
								
									
										
										
										
											2010-01-30 01:58:29 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    { $unchecked-example "USING: random prettyprint ;" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    "{ 1 2 3 } 2 sample ." 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    "{ 3 2 }" 
							 
						 
					
						
							
								
									
										
										
										
											2009-09-23 13:04:06 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-10-04 12:44:12 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								HELP:  delete-random 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ $values
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								     { "seq"  sequence  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								     { "elt"  object  } }
							 
						 
					
						
							
								
									
										
										
										
											2009-10-28 00:41:57 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $description "Deletes a random number from a sequence using "  { $link remove-nth!  } " and returns the deleted object."  } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2008-10-04 12:44:12 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								ARTICLE: "random-protocol"  "Random protocol" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								"A random number generator must implement one of these two words:" 
							 
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $subsections
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    random-32*
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    random-bytes*
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2008-10-04 12:44:12 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								"Optional, to seed a random number generator:" 
							 
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $subsections seed-random } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2008-10-04 12:44:12 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								ARTICLE: "random"  "Generating random integers" 
							 
						 
					
						
							
								
									
										
										
										
											2008-10-05 23:08:13 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								"The "  { $vocab-link "random"  } " vocabulary contains a protocol for generating random or pseudorandom numbers." 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								$nl
							 
						 
					
						
							
								
									
										
										
										
											2009-01-27 00:20:27 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								"The “Mersenne Twister” pseudorandom number generator algorithm is the default generator stored in "  { $link random-generator } "." 
							 
						 
					
						
							
								
									
										
										
										
											2008-10-05 23:08:13 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								$nl
							 
						 
					
						
							
								
									
										
										
										
											2008-10-04 12:44:12 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								"Generate a random object:" 
							 
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $subsections random }
							 
						 
					
						
							
								
									
										
										
										
											2009-09-30 16:56:02 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								"Efficient 32-bit random numbers:" 
							 
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $subsections random-32 }
							 
						 
					
						
							
								
									
										
										
										
											2008-10-04 12:44:12 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								"Combinators to change the random number generator:" 
							 
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $subsections
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    with-random
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    with-system-random
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    with-secure-random
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2008-10-04 12:44:12 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								"Implementation:" 
							 
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $subsections "random-protocol"  }
							 
						 
					
						
							
								
									
										
										
										
											2009-02-14 15:25:48 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								"Randomizing a sequence:" 
							 
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $subsections randomize }
							 
						 
					
						
							
								
									
										
										
										
											2009-09-23 13:24:50 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								"Sampling a sequences:" 
							 
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $subsections sample }
							 
						 
					
						
							
								
									
										
										
										
											2008-10-04 12:44:12 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								"Deleting a random element from a sequence:" 
							 
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $subsections delete-random }
							 
						 
					
						
							
								
									
										
										
										
											2009-05-10 14:39:08 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								"Random numbers with "  { $snippet "n"  } " bits:" 
							 
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ $subsections
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    random-bits
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    random-bits*
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} ;
 
							 
						 
					
						
							
								
									
										
										
										
											2008-10-04 12:44:12 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								ABOUT: "random"