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  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $values { "tuple"  "a random number generator"  } { "seed"  "an integer between 0 and 2^32-1"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Seed the random number generator."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $notes "Not supported on all random number generators."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  random-32*  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $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  
						 
					
						
							
								
									
										
										
										
											2008-10-05 23:08:13 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "seq"  sequence  } { "elt"  "a random element"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Outputs a random element of the input sequence. Outputs "  { $link f  } " if the sequence is empty."  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $notes "Since integers are sequences, passing an integer "  { $snippet "n"  } " outputs an integer in the interval "  { $snippet "[0,n)"  } "."  }
							 
						 
					
						
							
								
									
										
										
										
											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 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $values { "n"  "an integer"  } { "r"  "a random integer"  } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $description "Outputs an random integer n bits in length."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  delete-random  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $values
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								     { "seq"  sequence  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								     { "elt"  object  } }
							 
						 
					
						
							
								
									
										
										
										
											2008-10-05 16:14:05 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $description "Deletes a random number from a sequence using "  { $link delete-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:"  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection random-32* }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection random-bytes* }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Optional, to seed a random number generator:"  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection seed-random } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"The ``Mersenne Twister'' pseudorandom number generator algorithm is the default generator stored in "  { $link random-generator } "."  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$nl
							 
						 
					
						
							
								
									
										
										
										
											2008-10-04 12:44:12 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"Generate a random object:"  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection random }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Combinators to change the random number generator:"  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection with-random }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection with-system-random }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection with-secure-random }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Implementation:"  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection "random-protocol"  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Deleting a random element from a sequence:"  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ $subsection delete-random } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								ABOUT: "random"