Question abour rand()

Discussion in 'Ruby' started by Panagiotis Atmatzidis, Dec 29, 2009.

  1. Dear Sirs,

    I'm looking for a way to create a random sequence of numbers using the =
    'rand' method. I can think of something like:

    -----
    while pipi =3D=3D TRUE
    if x > 1950 || x < 2000
    return x
    pipi =3D FALSE
    ------

    Since however the book that I'm reading has not introduced while/else/if =
    etc. I'm thinking that there must be an easier and probably more =
    sensible way to approach this. As far as I've read rand(2000) will give =
    random numbers from 0-to-2000 no matter what.

    NOTE: I don't want to use another method just to do the job.

    thanks in advance & best regards



    Panagiotis (atmosx) Atmatzidis

    email:
    URL: http://www.convalesco.org
    GnuPG ID: 0xFC4E8BB4=20
    gpg --keyserver x-hkp://pgp.mit.edu --recv-keys 0xFC4E8BB4
    --
    The wise man said: "Never argue with an idiot. They bring you down to =
    their level and beat you with experience."
     
    Panagiotis Atmatzidis, Dec 29, 2009
    #1
    1. Advertising

  2. Hello,

    > I'm looking for a way to create a random sequence of numbers using the 'r=

    and' method. I can think of something like:
    >
    > -----
    > while pipi =3D=3D TRUE
    > =A0if x > 1950 || x < 2000


    Perhaps you were thinking of "and" rather than "or" because all number
    is either greater than 1950 or smaller than 2000.

    > Since however the book that I'm reading has not introduced while/else/if =

    etc. I'm thinking that there must be an easier and probably more sensible w=
    ay to approach this. As far as I've read rand(2000) will give random number=
    s from 0-to-2000 no matter what.

    Does 'rand(50) + 1950' do what you want ?

    Cheers,

    --=20
    JJ Fleck
    PCSI1 Lyc=E9e Kl=E9ber
     
    Fleck Jean-Julien, Dec 29, 2009
    #2
    1. Advertising

  3. Panagiotis Atmatzidis

    Seebs Guest

    On 2009-12-29, Panagiotis Atmatzidis <> wrote:
    > while pipi == TRUE
    > if x > 1950 || x < 2000
    > return x
    > pipi = FALSE


    Hmm. Looks as though you want the range 1951..1999. Well, that's easy
    enough:

    rand(49) + 1951

    (Note that rand(49) returns anything from 0 to 48, inclusive.)

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Dec 29, 2009
    #3
  4. Panagiotis Atmatzidis wrote:
    > Dear Sirs,
    >
    > I'm looking for a way to create a random sequence of numbers using the
    > 'rand' method. I can think of something like:
    >
    > -----
    > while pipi == TRUE
    > if x > 1950 || x < 2000
    > return x
    > pipi = FALSE
    > ------
    >
    > Since however the book that I'm reading has not introduced while/else/if
    > etc. I'm thinking that there must be an easier and probably more
    > sensible way to approach this. As far as I've read rand(2000) will give
    > random numbers from 0-to-2000 no matter what


    Use Seebs' suggestion.

    >
    > NOTE: I don't want to use another method just to do the job.


    Then you are probably being silly. It will make for more readable and
    more reusable code if you encapsulate this in a method. Having lots of
    short methods is generally a *good* thing.

    >
    > thanks in advance & best regards
    >
    >
    >
    > Panagiotis (atmosx) Atmatzidis
    >
    > email:
    > URL: http://www.convalesco.org
    > GnuPG ID: 0xFC4E8BB4
    > gpg --keyserver x-hkp://pgp.mit.edu --recv-keys 0xFC4E8BB4


    Best,
    --
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
     
    Marnen Laibow-Koser, Dec 29, 2009
    #4
  5. On 29.12.2009 18:00, Marnen Laibow-Koser wrote:

    >> NOTE: I don't want to use another method just to do the job.

    >
    > Then you are probably being silly. It will make for more readable and
    > more reusable code if you encapsulate this in a method. Having lots of
    > short methods is generally a *good* thing.


    "Walk before you run."

    --
    Phillip Gawlowski
     
    Phillip Gawlowski, Dec 29, 2009
    #5
  6. Hi,

    Am Dienstag, 29. Dez 2009, 18:51:53 +0900 schrieb Panagiotis Atmatzidis:
    > I'm looking for a way to create a random sequence of numbers using the 'rand' method. I can think of something like:
    >
    > -----
    > while pipi == TRUE
    > if x > 1950 || x < 2000
    > return x
    > pipi = FALSE
    > ------
    >
    > Since however the book that I'm reading has not introduced while/else/if etc. I'm thinking that there must be an easier and probably more sensible way to approach this. As far as I've read rand(2000) will give random numbers from 0-to-2000 no matter what.
    >
    > NOTE: I don't want to use another method just to do the job.
    >
    > thanks in advance & best regards


    The "rand" method has been mentioned. Here are just some
    considerations about the above code.

    You don't need a loop for calculating a random value. (There is a
    random algorithm below.) But when use a loop, then you either say
    "return" or leave it by a condition but not both.

    cond = true
    while cond do
    ...
    if some_condition then
    cond = false
    end
    ...
    end

    _or_

    loop do
    ...
    break if some_condition
    ...
    end

    Note that "break" can even return values:

    result = loop do
    break "hi"
    end
    result == "hi"

    And here is an example of a very simple random implementation:

    class R
    def initialize
    @r = 0
    end
    def nextval
    @r = ((@r * 0xabcd) + 0x73737) % 0x1_0000_0000
    @r >> 16
    end
    end
    r = R.new
    new_random_value = r.iterate
    random_value_below_fifty = r.iterate % 50

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    *
    Discover String#notempty? at <http://raa.ruby-lang.org/project/step>.
     
    Bertram Scharpf, Dec 29, 2009
    #6
  7. Panagiotis Atmatzidis wrote:
    > I'm looking for a way to create a random sequence of numbers using the
    > 'rand' method.


    Others have answered the 'random number between 1950 and 2000' bit.

    In terms of creating a "sequence" of these values, here are a couple of
    options to consider.

    (1) Build an array.

    a = []
    10.times { a << rand(50) }

    This array (a) is an object which can be returned, passed to other
    functions etc. The above code hard-codes the number of values to build
    (10), but you could choose this dynamically at run-time instead.

    (2) Yield the values.

    For code which wants to "return" multiple values, you can instead
    "yield" the values to a block which the caller provides. You can yield
    multiple times, thus calling the block multiple times. In other
    programming languages you might call this a "callback function"

    def generate_random(n=10)
    n.times do
    yield rand(50)
    end
    end

    generate_random do |r|
    puts "I got #{r}!"
    end

    Note that you can legitimately yield an infinite number of times,
    because the block can abort the sequence when it wants. So in the
    following example, the user of the function decides when they don't want
    any more random numbers:

    def generate_infinite
    loop do
    yield rand(50)
    end
    end

    count = 0
    generate_infinite do |r|
    puts "I got #{r}!"
    count += 1
    break unless count < 10
    end

    A more advanced way to do this is with an 'Enumerator' object, but I'd
    say you should start with the above for now.
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Dec 29, 2009
    #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. daniel kaplan

    rand() question

    daniel kaplan, Sep 15, 2004, in forum: Perl
    Replies:
    4
    Views:
    634
    Ian Sedwell
    Sep 21, 2004
  2. hedylogus

    Beginner Question--rand( )

    hedylogus, Jul 25, 2003, in forum: C++
    Replies:
    23
    Views:
    756
    Cy Edmunds
    Jul 28, 2003
  3. Frank Mitchell

    Confused abour 'ri' in 1.8.1?

    Frank Mitchell, Jan 2, 2004, in forum: Ruby
    Replies:
    9
    Views:
    102
    Dave Thomas
    Jan 2, 2004
  4. 7stud --

    rand() v. rand(0.1) ?

    7stud --, Sep 15, 2007, in forum: Ruby
    Replies:
    6
    Views:
    246
    Morton Goldberg
    Sep 16, 2007
  5. Marek Stepanek
    Replies:
    8
    Views:
    125
    Marek Stepanek
    Aug 16, 2006
Loading...

Share This Page