Generating Random Numbers


You want to generate pseudorandom numbers, select items from a data structure at random, or repeatedly generate the same "random" numbers for testing purposes.


Use the Kernel#rand function with no arguments to select a psuedorandom floating-point number from a uniform distribution between 0 and 1.

	rand # => 0.517297883846589
	rand # => 0.946962603814814

Pass in a single integer argument n to Kernel#rand, and it returns an integer between 0 and n1:

	rand(5) # => 0
	rand(5) # => 4
	rand(5) # => 3
	rand(1000) # => 39



You can use the single-argument form of Kernel#rand to build many common tasks based on randomness. For instance, this code selects a random item from an array.

	a = ['item1', 'item2', 'item3']
	a[rand(a.size)] # => "item3"

To select a random key or value from a hash, turn the keys or values into an array and select one at random.

	m = { :key1 => 'value1',
	 :key2 => 'value2', 
	 :key3 => 'value3' }
	values = m.values
	values[rand(values.size)] # => "value1"

This code generates pronounceable nonsense words:

	def random_word 
	 letters = { ?v => 'aeiou',
	 ?c => 'bcdfghjklmnprstvwyz' }
	 word = '' 
	 'cvcvcvc'.each_byte do |x| 
	 source = letters[x]
	 word << source[rand(source.length)].chr
	 return word

	random_word # => "josuyip" 
	random_word # => "haramic" 

The Ruby interpreter initializes its random number generator on startup, using a seed derived from the current time and the process number. To reliably generate the same random numbers over and over again, you can set the random number seed manually by calling the Kernel#srand function with the integer argument of your choice. This is useful when you're writing automated tests of "random" functionality:

	#Some random numbers based on process number and current time
	rand(1000) # => 187
	rand(1000) # => 551
	rand(1000) # => 911

	#Start the seed with the number 1
	srand 1
	rand(1000) # => 37 
	rand(1000) # => 235
	rand(1000) # => 908

	#Reset the seed to its previous state
	srand 1
	rand(1000) # => 37 
	rand(1000) # => 235
	rand(1000) # => 908


See Also

  • Recipe 4.10, "Shuffling an Array"
  • Recipe 5.11, "Choosing Randomly from a Weighted List"
  • Recipe 6.9, "Picking a Random Line from a File"
  • The Facets library implements many methods for making random selections from data structures: Array#pick, Array#rand_subset, Hash#rand_pair, and so on; it also defines String.random for generating random strings
  • Christian Neukirchen's rand.rb also implements many random selection methods (



Date and Time



Files and Directories

Code Blocks and Iteration

Objects and Classes8

Modules and Namespaces

Reflection and Metaprogramming


Graphics and Other File Formats

Databases and Persistence

Internet Services

Web Development Ruby on Rails

Web Services and Distributed Programming

Testing, Debugging, Optimizing, and Documenting

Packaging and Distributing Software

Automating Tasks with Rake

Multitasking and Multithreading

User Interface

Extending Ruby with Other Languages

System Administration

Ruby Cookbook
Ruby Cookbook (Cookbooks (OReilly))
ISBN: 0596523696
EAN: 2147483647
Year: N/A
Pages: 399 © 2008-2020.
If you may any questions please contact us: