Generate random numbers : rand « Development « Ruby






Generate random numbers


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

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

 








Related examples in the same category

1.Generating Random Numbers
2.Random with a range
3.Get a random item in an array
4.Get a random value in a hash
5.Some random numbers based on process number and current time
6.Start the seed with the number 1