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. 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
rand(2000-60)+60
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)
On Mar 28, 2:51 pm, Daddek D. [email protected] 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
[email protected] 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”.
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.
Phillip G. wrote:
In which situation is the difference important?
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)
http://borasky-research.blogspot.com/
If God had meant for carrots to be eaten cooked, He would have given
rabbits fire.
Kyle S. wrote:
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.
On 3/29/07, Phillip G. [email protected] wrote:
In which situation is the difference important?
My guess is, that it is cryptography, to make a salt in a key less
“guessable”.
Cryptography depends critically on high-quality (“real”) randomness in
a
variety of areas. Without it you have no crypto!
Last, but not least: Where would I go to create real random numbers,
preferably in a platform-independent way?
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. (Or the traditional standby, typing or moving a
mouse at random.) On many Linux platforms, you can get a limited amount
of
good-quality noise
by reading /dev/random, if it’s implemented on your particular hardware.
(/dev/urandom does not block but does not guarantee high entropy, and is
not
suitable for crypto.) Be careful if you use it for more than a few
thousand
bytes a minute. It can and will block. For a good long time, too. It
that
happens to you, you can generate noise at a somewhat higher rate by
continually reading and writing very large disk files (taking care that
the
drive hardware and not the driver cache is getting exercised).
Platform-independent: forget about it.
Francis C. 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)
http://borasky-research.blogspot.com/
If God had meant for carrots to be eaten cooked, He would have given
rabbits fire.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs