[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

HOW-TO produce random numbers within a range?

Ruby N00bie

1/6/2007 6:19:00 PM

Hello,
Thanks for reading this.
I am very new to Ruby and have been learning using a tutorial at
http://pine.fm/Learn...
by Chris (thanks Chris)
After searching fairly thoroughly in forums and tutorials I found no
answer to my problem.
I have come to a point where i need to generate a random number within a
range.

I have learnt that rand(100) produces random numbers from 0 to 99 but i
need to know how to set a minimum for the random number.

Your help would be greatly appreciated.

Thanks in advance, tckl.

--
Posted via http://www.ruby-....

5 Answers

matt

1/6/2007 6:44:00 PM

0

Ruby N00bie <ralphallan@gmail.com> wrote:

> Hello,
> Thanks for reading this.
> I am very new to Ruby and have been learning using a tutorial at
> http://pine.fm/Learn...
> by Chris (thanks Chris)
> After searching fairly thoroughly in forums and tutorials I found no
> answer to my problem.
> I have come to a point where i need to generate a random number within a
> range.
>
> I have learnt that rand(100) produces random numbers from 0 to 99 but i
> need to know how to set a minimum for the random number.

Suppose the range is 50-99.

Generate a random number between 0 and 50, and add 50 to the result.

m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Rimantas Liubertas

1/6/2007 6:48:00 PM

0

<...>
> I have learnt that rand(100) produces random numbers from 0 to 99 but i
> need to know how to set a minimum for the random number.
>
> Your help would be greatly appreciated.
<...>

Add the minimum :)

def range_rand(min,max)
min + rand(max-min)
end


--
Regards,
Rimantas
--
http://rim...

Ruby N00bie

1/6/2007 6:56:00 PM

0

Thanks very much all three of you.
That's all I needed to know.
tckl

--
Posted via http://www.ruby-....

Rodrigo Bermejo

1/6/2007 7:02:00 PM

0

Welcome abord !


Easier to make it fit it that fit your self.
The Ultimate Morphing Lego Game.


#!/usr/bin/my_new_lego_game

alias old_rand rand

def rand(min,max)
until min < r=old_rand(max); end
return r
end


10.times do |x|
p rand(1,10)
end


-.rb

--
Posted via http://www.ruby-....

William James

1/6/2007 7:05:00 PM

0

Ruby N00bie wrote:
> Hello,
> Thanks for reading this.
> I am very new to Ruby and have been learning using a tutorial at
> http://pine.fm/Learn...
> by Chris (thanks Chris)
> After searching fairly thoroughly in forums and tutorials I found no
> answer to my problem.
> I have come to a point where i need to generate a random number within a
> range.
>
> I have learnt that rand(100) produces random numbers from 0 to 99 but i
> need to know how to set a minimum for the random number.
>
> Your help would be greatly appreciated.
>
> Thanks in advance, tckl.
>
> --
> Posted via http://www.ruby-....

You don't need to be able to set a minimum. You simply take what
rand() gives you and add an offset.

>> 25.times{ r = rand(10); print "#{ r } " }
5 1 0 5 4 3 5 0 4 0 7 8 6 8 6 9 7 1 7 9 9 3 5 7 5 => 25
>> 25.times{ r = rand(10); print "#{ r + 50 } " }
57 51 51 58 54 53 56 58 54 57 54 56 57 58 50 56 56 50 58 52 58 55 53 50
56 => 25