 
 This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.
| Generating Random Numbers | ||
|---|---|---|
| Code | Expected | Actual | 
| rand | 0.517297883846589 | 0.517297883846589 | 
| rand | 0.946962603814814 | 0.946962603814814 | 
| rand(5) | 0 | 0 | 
| rand(5) | 4 | 4 | 
| rand(5) | 3 | 3 | 
| rand(1000) | 39 | 39 | 
| a = ['item1', 'item2', 'item3'] a[rand(a.size)] | "item3" | "item3" | 
| m = { :key1 => 'value1',
      :key2 => 'value2',
      :key3 => 'value3' }
values = m.values
values[rand(values.size)] | "value1" | "value1" | 
| 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" | "josuyip" | 
| random_word | "haramic" | "haramic" | 
| #Some random numbers based on process number and current time rand(1000) | 187 | 187 | 
| rand(1000) | 551 | 551 | 
| rand(1000) | 911 | 911 | 
| #Start the seed with the number 1 srand 1 rand(1000) | 37 | 37 | 
| rand(1000) | 235 | 235 | 
| rand(1000) | 908 | 908 | 
| #Reset the seed to its previous state srand 1 rand(1000) | 37 | 37 | 
| rand(1000) | 235 | 235 | 
| rand(1000) | 908 | 908 |