Simple question on random numbers

Discussion in 'Ruby' started by Daddek Daddek, Mar 28, 2007.

  1. 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/.
     
    Daddek Daddek, Mar 28, 2007
    #1
    1. Advertising

  2. Daddek Daddek

    SonOfLilit Guest

    rand(2000-60)+60

    On 3/28/07, Daddek Daddek <> 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/.
    >
    >
     
    SonOfLilit, Mar 28, 2007
    #2
    1. Advertising

  3. Daddek Daddek

    Matthew Moss Guest

    > 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)
     
    Matthew Moss, Mar 29, 2007
    #3
  4. Daddek Daddek

    Guest

    On Mar 28, 2:51 pm, Daddek Daddek <> 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
     
    , Mar 29, 2007
    #4
  5. 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".
     
    Phillip Gawlowski, Mar 29, 2007
    #5
  6. Phillip Gawlowski wrote:
    > 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.
     
    M. Edward (Ed) Borasky, Mar 29, 2007
    #6
  7. Daddek Daddek

    Kyle Schmitt Guest

    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.
     
    Kyle Schmitt, Mar 29, 2007
    #7
  8. 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/
     
    Reid Thompson, Mar 30, 2007
    #8
  9. Daddek Daddek

    Daddek .. Guest

    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/.
     
    Daddek .., Mar 30, 2007
    #9
  10. 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.
     
    M. Edward (Ed) Borasky, Mar 30, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. globalrev
    Replies:
    4
    Views:
    823
    Gabriel Genellina
    Apr 20, 2008
  2. Alex Untitled
    Replies:
    11
    Views:
    688
    Giampiero Zanchi
    Nov 16, 2009
  3. PerlFAQ Server

    FAQ 4.10 Why aren't my random numbers random?

    PerlFAQ Server, Feb 12, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    235
    PerlFAQ Server
    Feb 12, 2011
  4. PerlFAQ Server

    FAQ 4.10 Why aren't my random numbers random?

    PerlFAQ Server, Apr 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    270
    PerlFAQ Server
    Apr 27, 2011
  5. VK
    Replies:
    15
    Views:
    1,337
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page