get a uniformly distributed random number in ruby

You can use the rand method in Ruby to generate a uniformly distributed random number. By default, it generates a random float between 0.0 and 1.0.

main.rb
# Generate a random float between 0.0 and 1.0
rand

# Generate a random integer between 0 and 9
rand(10)

# Generate a random float between -5.0 and 5.0
rand(-5.0..5.0)

# Generate a random integer between 0 and 100
rand(101)
226 chars
12 lines

If you need to generate a random number within a specific range, you can use the rand method with a range object as an argument. The range object can be created using the .. operator, and you can specify the lower and upper bounds of the range.

By providing an integer as an argument to the rand method, you can generate a random integer within the specified range, from 0 up to, but not including, the provided integer.

Keep note that if you want to set the seed for the random number generator, you'll want to call srand with a seed value before calling rand.

gistlibby LogSnag