Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Simple question on random numbers

Reply
Thread Tools

Simple question on random numbers

 
 
Daddek Daddek
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/.

 
Reply With Quote
 
 
 
 
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/.
>
>


 
Reply With Quote
 
 
 
 
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)

 
Reply With Quote
 
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

 
Reply With Quote
 
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".

 
Reply With Quote
 
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)
http://borasky-research.blogspot.com/

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


 
Reply With Quote
 
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.

 
Reply With Quote
 
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/

 
Reply With Quote
 
Daddek ..
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/.

 
Reply With Quote
 
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)
http://borasky-research.blogspot.com/

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 4.10 Why aren't my random numbers random? PerlFAQ Server Perl Misc 0 04-27-2011 04:00 AM
FAQ 4.10 Why aren't my random numbers random? PerlFAQ Server Perl Misc 0 02-12-2011 11:00 PM
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
How do I get a random number between two random numbers? Alex Untitled Ruby 11 11-16-2009 09:45 AM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM



Advertisments