lib - generating a list of random number

Discussion in 'Ruby' started by Josselin, Nov 14, 2006.

  1. Josselin

    Josselin Guest

    I try to generate an array of N random numbers in range between 2
    integers min and max
    but I don't understand very well how to coordinate the sue of srand and rand..
    is there an already written function for that ? and btw is there any
    place to find such useful functions ?

    thanks

    joss
    Josselin, Nov 14, 2006
    #1
    1. Advertising

  2. On 14.11.2006 17:09, Josselin wrote:
    > I try to generate an array of N random numbers in range between 2
    > integers min and max
    > but I don't understand very well how to coordinate the sue of srand and
    > rand..


    I am not sure I understand you here. What does "sue" mean here?

    > is there an already written function for that ? and btw is there any
    > place to find such useful functions ?


    What about this?

    >> def rand_seq(count, min, max)
    >> (1..count).map { rand(max-min) + min }
    >> end

    => nil
    >> rand_seq 10, -5, 5

    => [2, -4, -2, -1, -3, 1, 1, 1, -3, 0]

    Kind regards

    robert
    Robert Klemme, Nov 14, 2006
    #2
    1. Advertising

  3. Josselin

    Guest

    On Wed, 15 Nov 2006, Josselin wrote:

    > I try to generate an array of N random numbers in range between 2 integers
    > min and max
    > but I don't understand very well how to coordinate the sue of srand and
    > rand..
    > is there an already written function for that ? and btw is there any place
    > to find such useful functions ?
    >
    > thanks
    >
    > joss
    >
    >
    >


    harp:~ > cat a.rb
    n, max, min = 42, 10, 6
    p Array.new(n).map{ rand((max - min) + 1) + min}


    harp:~ > ruby a.rb
    [6, 8, 8, 10, 9, 7, 6, 8, 9, 7, 10, 10, 7, 7, 9, 9, 6, 9, 10, 6, 8, 10, 6, 9, 7, 8, 8, 10, 8, 9, 9, 9, 10, 7, 9, 6, 6, 8, 8, 10, 9, 7]



    -a
    --
    y religion is very simple. my religion is kindness. -- the dalai lama
    , Nov 14, 2006
    #3
  4. Josselin

    Josselin Guest

    On 2006-11-14 17:30:31 +0100, Robert Klemme <> said:

    > On 14.11.2006 17:09, Josselin wrote:
    >> I try to generate an array of N random numbers in range between 2
    >> integers min and max
    >> but I don't understand very well how to coordinate the sue of srand and rand..

    >
    > I am not sure I understand you here. What does "sue" mean here?
    >
    >> is there an already written function for that ? and btw is there any
    >> place to find such useful functions ?

    >
    > What about this?
    >
    > >> def rand_seq(count, min, max)
    > >> (1..count).map { rand(max-min) + min }
    > >> end

    > => nil
    > >> rand_seq 10, -5, 5

    > => [2, -4, -2, -1, -3, 1, 1, 1, -3, 0]
    >
    > Kind regards
    >
    > robert


    sorry sue => usage.... typing mistake...

    thanks !!! that's exactly what I searched for...
    I knew how to generate an array with an 'upper limit only' ...
    Josselin, Nov 14, 2006
    #4
  5. Josselin

    Josselin Guest

    On 2006-11-14 18:17:19 +0100, Paul Lutus <> said:

    > Josselin wrote:
    >
    >> I try to generate an array of N random numbers in range between 2
    >> integers min and max
    >> but I don't understand very well how to coordinate the sue of srand and
    >> rand..

    >
    > They are very easy to use, but first you have to decide what you want to do.
    > What do you want to do? What role does "srand()" play? Be specific.
    >
    >> is there an already written function for that ?

    >
    > For what, specifically? The range of random numbers, or the use of
    > "srand()"? If you always want the same sequence of random numbers, then
    > there is one solution, but if you always want a different sequence, there
    > is a different solution. Which do you want?
    >
    >> and btw is there any place to find such useful functions ?

    >
    > There sure is:
    >
    > http://www.ruby-doc.org/

    thanks
    >
    > By the way, when you ask a question like this, it is best to express your
    > requirement as specifically as possible. You may have noticed that you got
    > two different replies to your inquiry, each with sample code that provided
    > differing interpretations of your request.
    >
    > The solution to this problem is to leave no doubt about your requirement.
    > For example, when you say "between two integers" do you mean:
    >
    > a < n < b
    >
    > or
    > a <= n <= b
    >
    > or some third possibility?


    I apologize... I was looking for a result (how to generate a list of
    random integer numbers between a minimum and a maximum .. included) ...
    yes forgotten to specify including limits....

    and at the same time trying to understand how to USE srand()and rand(),
    my standard Ruby manual is clearly written but there is no example for
    this function.
    I googled a lot but without any success regarding examples...
    my first thought was: srand() is useful in setting a limit to the
    generator.... used by rand() so it could have been a clue to solve my
    problem...
    Josselin, Nov 14, 2006
    #5
  6. Josselin

    Guest

    On Wed, 15 Nov 2006, Josselin wrote:

    > I googled a lot but without any success regarding examples...
    > my first thought was: srand() is useful in setting a limit to the
    > generator.... used by rand() so it could have been a clue to solve my
    > problem...


    Use ruby-doc.org

    http://ruby-doc.org/core/classes/Kernel.html#M002012


    Kirk Haines
    , Nov 14, 2006
    #6
  7. Josselin

    Josselin Guest

    On 2006-11-14 19:25:09 +0100, Keynan Pratt <> said:

    > srand refers to seed rand
    > rand is not generating a random number it is just picing a number from
    > a list of huge size srand says start from this position so passing in
    > the time.now before you start looping you will get more random results


    thanks a lot ! got it ...
    Josselin, Nov 15, 2006
    #7
    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. Leon

    Random Number Generating

    Leon, Oct 18, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    456
    Kevin Spencer
    Oct 18, 2004
  2. Wally
    Replies:
    1
    Views:
    2,778
    pvdg42
    Mar 20, 2006
  3. John Tsiombikas (Nuclear / the Lab)

    Re: Generating a random number

    John Tsiombikas (Nuclear / the Lab), Jul 1, 2003, in forum: C++
    Replies:
    1
    Views:
    352
    David White
    Jul 1, 2003
  4. globalrev
    Replies:
    4
    Views:
    745
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,125
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page