Tuesday, August 21, 2007

Ruby Random

Problem

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

Solution

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

Discussion

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
end
return word
end

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

No comments:

analytics