Random

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.

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

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Ruby on Codecademy