 # Generating a random floating point number

I’m a Ruby newbie and I’m wondering if there’s a simple way to generate
a random floating point number, I want to generate numbers between 0
and about 2-3.6 with the max changing each time (an example max would
be 3.26861475366199).

I’m hoping there’s something simpler than generating an integer
(rand(3)) and then generating a point (rand(0.268)) and adding them
together…

Thanks for any advice you can give!

(I’m really loving Ruby, I rewrote some of my university Java work and
programs that took hours to write in Java were done in minutes!)

jamiethehutt [email protected] wrote:

I’m a Ruby newbie and I’m wondering if there’s a simple way to generate
a random floating point number, I want to generate numbers between 0
and about 2-3.6 with the max changing each time (an example max would
be 3.26861475366199).

I’m hoping there’s something simpler than generating an integer
(rand(3)) and then generating a point (rand(0.268)) and adding them
together…

Calling Kernel.rand with no argument yields a random floating point
number n where (0 <= n < 1). So:

my_number = rand * my_limit

…should help.

HTH,
Tim H.

jamiethehutt wrote:

I’m a Ruby newbie and I’m wondering if there’s a simple way to generate
a random floating point number, I want to generate numbers between 0
and about 2-3.6 with the max changing each time (an example max would
be 3.26861475366199).

That’s a pretty weird request, but you’d do something like this:
rand*(2+rand*1.6)

Daniel

On Wed, 12 Jul 2006, jamiethehutt wrote:

I’m a Ruby newbie and I’m wondering if there’s a simple way to generate
a random floating point number, I want to generate numbers between 0
and about 2-3.6 with the max changing each time (an example max would
be 3.26861475366199).

I’m hoping there’s something simpler than generating an integer
(rand(3)) and then generating a point (rand(0.268)) and adding them
together…

Thanks for any advice you can give!

def fp_rand(limit)
fl = limit.floor
rm = limit.remainder(fl)
rand(fl) + rand(rm)
end

fp_rand(12.73)

=> 10.1895547681143

Kirk H.

Tim H. wrote:

my_number = rand * my_limit

Thanks! That seems to be just what I need!

Jamiethehutt

On Thu, Jul 13, 2006 at 12:39:50AM +0900, [email protected] wrote:

def fp_rand(limit)
fl = limit.floor
rm = limit.remainder(fl)
rand(fl) + rand(rm)
end

fp_rand(12.73)
=> 10.1895547681143

That method would never return values whose decimal part >= 0.73 in the above example. Besides, the variance of the resulting random variable would be lower than for the correct limit * rand(): (fl^2 + rm^2) / 12 instead of (fl + rm)^2 / 12 = limit^2 / 12

On Thu, 13 Jul 2006, Mauricio F. wrote:

That method would never return values whose decimal part >= 0.73 in the above example. Besides, the variance of the resulting random variable would be lower than for the correct limit * rand(): (fl^2 + rm^2) / 12 instead of (fl + rm)^2 / 12 = limit^2 / 12

Thanks, Mauricio. It’s always good to have one’s nits picked on
occasion. Kirk

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs