Velocity Reviews > Ruby > Simple question on random numbers

# Simple question on random numbers

Guest
Posts: n/a

 03-28-2007
Hi. Total noob, very simple question. Can anyone tell me how I can
generate a random number between two specific figures (between 60 and
2000, for instance)/

Thanks

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

SonOfLilit
Guest
Posts: n/a

 03-28-2007
rand(2000-60)+60

On 3/28/07, Daddek Daddek <(E-Mail Removed)> wrote:
> Hi. Total noob, very simple question. Can anyone tell me how I can
> generate a random number between two specific figures (between 60 and
> 2000, for instance)/
>
> Thanks
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

Matthew Moss
Guest
Posts: n/a

 03-29-2007
> Hi. Total noob, very simple question. Can anyone tell me how I can
> generate a random number between two specific figures (between 60 and
> 2000, for instance)/

def rand_ie(lo, hi) # domain is [lo, hi)
lo + rand(hi - lo)
end

def rand_ii(lo, hi) # domain is [lo, hi]
rand_ie(lo, hi + 1)
end

rand_ii(60, 2000)

cbinckly@gmail.com
Guest
Posts: n/a

 03-29-2007
On Mar 28, 2:51 pm, Daddek Daddek <(E-Mail Removed)> wrote:
> Hi. Total noob, very simple question. Can anyone tell me how I can
> generate a random number between two specific figures (between 60 and
> 2000, for instance)/
>
> Thanks
>
> --
> Posted viahttp://www.ruby-forum.com/.

Hi,

A good place to start when you're new and don't know is the
documentation. Thankfully the entire ruby API, which outlines all the
libraries and modules that come both as core Ruby libraries and
standard Ruby libraries. Try www.ruby-doc.org, and have a look at the
Core API. Specifically, you're going to want the Kernel module.

Have a look at the rand method. It will not allow you to specify a
range, but will generate a number between 0 and (max1 - 1). If you
make ((max1 - 1) - 0) equal to your range (which in the case of your
example is 2000 - 60 = 1940 + 1 = 1941), and then scale it up by 60,
youll always end up with a random number in the range you're looking
for.

So, a set of calls like this:

R_OFFSET = 60
random_num = Kernel.rand(1941) + R_OFFSET

will give you a random number in the range 60-2000. Try and use a
constant (R_OFFSET) to handle the offset, no one likes magic numbers.

Keep in mind that Ruby's random numbers are only pseudorandom, so if
you need real random numbers you should look into something a little
more robust than Kernel.rand

Thanks,

cb

Phillip Gawlowski
Guest
Posts: n/a

 03-29-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Keep in mind that Ruby's random numbers are only pseudorandom, so if
> you need real random numbers you should look into something a little
> more robust than Kernel.rand

Three questions:
What is the difference between real random numbers and pseuderandom numbers?

In which situation is the difference important?
My guess is, that it is cryptography, to make a salt in a key less
"guessable".

Last, but not least: Where would I go to create real random numbers,
preferably in a platform-independent way?

P.S.: Call me lazy, but I wouldn't know what to search for, much less be
able to weight the sources I get. Feel free to reply off-list, if this
is too off-topic for the list. In that case, I'll compile an article and
make it publicly available.

--
Phillip "CynicalRyan" Gawlowski
http://cynicalryan.110mb.com/

Rule of Open-Source Programming #11:

When a developer says he will work on something, he or she means
"maybe".

M. Edward (Ed) Borasky
Guest
Posts: n/a

 03-29-2007
Phillip Gawlowski wrote:
> (E-Mail Removed) wrote:
>
>> Keep in mind that Ruby's random numbers are only pseudorandom, so if
>> you need real random numbers you should look into something a little
>> more robust than Kernel.rand

>
> Three questions:
> What is the difference between real random numbers and pseuderandom
> numbers?
>
> In which situation is the difference important?
> My guess is, that it is cryptography, to make a salt in a key less
> "guessable".
>
> Last, but not least: Where would I go to create real random numbers,
> preferably in a platform-independent way?
>
> P.S.: Call me lazy, but I wouldn't know what to search for, much less
> be able to weight the sources I get. Feel free to reply off-list, if
> this is too off-topic for the list. In that case, I'll compile an
> article and make it publicly available.
>

People actually still do masters' theses on such topics, though
thankfully not PhDs. Seriously, though, there's a big section on the
subject in Knuth's "Art of Computer Programming", volume 2 IIRC. As far
as computing "real" random numbers, I don't know if there's a platform
independent way yet short of buying hardware and a driver for said
hardware, but there is certainly a lot of literature available for the
platform-dependent solutions in Linux. My best guess is that the people
who need such hardware probably wouldn't appreciate you poking around
without a clearance.

--
M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P)

If God had meant for carrots to be eaten cooked, He would have given rabbits fire.

Kyle Schmitt
Guest
Posts: n/a

 03-29-2007
This is going to sound like a smartass answer, but real random numbers
are really random, psudo random numbers are random-ish.

Most "random" number generators are actually periodic, even if they
have large periods. I could be wrong, but having to specify a seed is
a clue it's psudo random. Seeding a "random" number generator is a
way to increase it's randomness somewhat. With the good old C rand
function, try seeding it with 10 (using srand) and write it out to a
file. Run it as many times as you want, and you'll end up with the
SAME file.

Some modern computers have real entropy sources built in, and for
those that don't, it's possible to collect entropy from various places
(you'll have to do some googling about openbsd since they go nuts with
this). You can use an entropy source as a _real_ random number
generator, or to seed a psudo random number generator to make it a
little less periodic.

If you want to be lazy there used to be a series of books called
numerical recipies in %w{c pascal youre-favorite-language}. One thing
they showed was a good random number generator, and apparently they
went into some depth on the subject.

Places where random vs psudo random are important:
If you are running models that use random numbers (a friend of mine
was going really neurotic once trying to make sure his supernovas
modeller was really really random)
Cryptography
umm... uhh... yea I can't think of more right now.

Reid Thompson
Guest
Posts: n/a

 03-30-2007
Kyle Schmitt wrote:
> This is going to sound like a smartass answer, but real random numbers
> are really random, psudo random numbers are random-ish.
>
> Most "random" number generators are actually periodic, even if they
> have large periods. I could be wrong, but having to specify a seed is
> a clue it's psudo random. Seeding a "random" number generator is a
> way to increase it's randomness somewhat. With the good old C rand
> function, try seeding it with 10 (using srand) and write it out to a
> file. Run it as many times as you want, and you'll end up with the
> SAME file.
>
> Some modern computers have real entropy sources built in, and for
> those that don't, it's possible to collect entropy from various places
> (you'll have to do some googling about openbsd since they go nuts with
> this). You can use an entropy source as a _real_ random number
> generator, or to seed a psudo random number generator to make it a
> little less periodic.
>
> If you want to be lazy there used to be a series of books called
> numerical recipies in %w{c pascal youre-favorite-language}. One thing
> they showed was a good random number generator, and apparently they
> went into some depth on the subject.
>
> Places where random vs psudo random are important:
> If you are running models that use random numbers (a friend of mine
> was going really neurotic once trying to make sure his supernovas
> modeller was really really random)
> Cryptography
> umm... uhh... yea I can't think of more right now.
>

http://realrand.rubyforge.org/

Guest
Posts: n/a

 03-30-2007
unknown wrote:

> Have a look at the rand method. It will not allow you to specify a
> range, but will generate a number between 0 and (max1 - 1). If you
> make ((max1 - 1) - 0) equal to your range (which in the case of your
> example is 2000 - 60 = 1940 + 1 = 1941), and then scale it up by 60,
> youll always end up with a random number in the range you're looking
> for.

Excellent. Thanks very much for your reply, it's a nice welcome to the
forum

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

M. Edward (Ed) Borasky
Guest
Posts: n/a

 03-30-2007
Francis Cianfrocca wrote:
> I'm sure someone will disagree with this but real randomness generally
> requires exposure to a physical process of some kind. Things like thermal
> noise inside a processor chip, disk drive seek times, or the interpacket
> times on a network link.

Well ... I'll agree that you need physical processes to generate "real
randomness" -- I think my original post said as much. But I'd be very
careful with disk seek times or interpacket times on a network link,
especially the latter. When one wants noise, one needs to know the
distribution of that noise. Interpacket times have a very bizarre
distribution, which you'll need to account for, and they are correlated
with each other, with the time of day, and a whole bunch of other stuff.
*This* they do still give PhDs for.

Curiously enough, one of the fairly recent PhDs in this area works at
Seagate.

> Platform-independent: forget about it.

Also forget about high-quality and inexpensive at the same time.

--
M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P)

If God had meant for carrots to be eaten cooked, He would have given rabbits fire.