How to best create a random float in a range between two floats

Pass a range of floats to rand

If you want to “create a random float in a range between two floats”, just pass a range of floats to rand.

rand(11.2...76.9)

(Tested with Ruby 2.1)

Edit

According to the documentation: https://ruby-doc.org/core-2.4.0/Random.html

There are two different ways to write the random function: inclusive and exclusive for the last value

rand(5..9)      # => one of [5, 6, 7, 8, 9]
rand(5...9)     # => one of [5, 6, 7, 8]
rand(5.0..9.0)  # => between 5.0 and 9.0, including 9.0
rand(5.0...9.0) # => between 5.0 and 9.0, excluding 9.0

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)