Simple question on random numbers

D

Daddek Daddek

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
 
M

Matthew Moss

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)
 
C

cbinckly

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

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
 
P

Phillip Gawlowski

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

M. Edward (Ed) Borasky

Phillip said:
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. :)
 
K

Kyle Schmitt

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.
 
R

Reid Thompson

Kyle said:
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/
 
D

Daddek ..

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
 
M

M. Edward (Ed) Borasky

Francis said:
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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top