Ruby Random

christian.dinh's avatar
Published Jul 27, 2021Updated Sep 9, 2021
Contribute to Docs

The rand function will return a random number greater than or equal to 0 and less than the integer passed to the function. If an argument is not passed to the function, the return value is a random number between 0 and 1.

  • Learn to program in Ruby, a flexible and beginner-friendly language used to create sites like Codecademy.
    • Beginner Friendly.
      9 hours
  • Learn the basics of building applications with this convenient and powerful web development framework.
    • With Certificate
    • Intermediate.
      6 hours

Example

# When no argument is passed, the result is a random number between 0 and 1.
puts rand();
# Output example: 0.5
# When an argument is passed, the result is a random number between 0 and the argument.
puts rand(10);
# Output example: 6

All contributors

Contribute to Docs

Learn Ruby on Codecademy

  • Learn to program in Ruby, a flexible and beginner-friendly language used to create sites like Codecademy.
    • Beginner Friendly.
      9 hours
  • Learn the basics of building applications with this convenient and powerful web development framework.
    • With Certificate
    • Intermediate.
      6 hours