Random integer within a range?

Discussion in 'Ruby' started by Reiichi Tyrael, Mar 1, 2010.

  1. I must create a little game; the one who play it must choose 2 numbers,
    the beginning and the end of a range, and the program generate a secret
    random number within that range, so the player must guess it!
    But the problem is...how do I generate a random number?
    I though at something like this:

    number_to_guess = rand(n1..n2)

    but it give me the error message "in `rand': can't convert Range into
    Integer (TypeError)"

    What can I do? Thanks!
    --
    Posted via http://www.ruby-forum.com/.
    Reiichi Tyrael, Mar 1, 2010
    #1
    1. Advertising

  2. Reiichi Tyrael,

    Is the range inclusive OR exclusive?

    Should n1 OR n2 be reurned sometimes or never?

    > number_to_guess = rand(n1..n2)



    --
    Posted via http://www.ruby-forum.com/.
    David Springer, Mar 1, 2010
    #2
    1. Advertising

  3. David Springer wrote:
    > Reiichi Tyrael,
    >
    > Is the range inclusive OR exclusive?
    >
    > Should n1 OR n2 be reurned sometimes or never?
    >
    >> number_to_guess = rand(n1..n2)

    Inclusive!

    --
    Posted via http://www.ruby-forum.com/.
    Reiichi Tyrael, Mar 1, 2010
    #3
  4. Reiichi Tyrael wrote:

    > Inclusive!


    Okay, I'll assume that these are two different positive integers.

    Try this:

    number_to_guess = (n2 > n1) ? n1 + rand((n2-n1+1)) : n2 +
    rand((n1-n2+1))
    --
    Posted via http://www.ruby-forum.com/.
    David Springer, Mar 1, 2010
    #4
  5. On 01.03.2010 20:21, Reiichi Tyrael wrote:
    > I must create a little game; the one who play it must choose 2 numbers,
    > the beginning and the end of a range, and the program generate a secret
    > random number within that range, so the player must guess it!
    > But the problem is...how do I generate a random number?
    > I though at something like this:
    >
    > number_to_guess = rand(n1..n2)
    >
    > but it give me the error message "in `rand': can't convert Range into
    > Integer (TypeError)"
    >
    > What can I do? Thanks!


    rand only accepts an integer which will make it produce values in the
    range 0...n (exclusive). So you can do:

    irb(main):001:0> low, high = 12, 56
    => [12, 56]
    irb(main):002:0> r = low + rand(high - low)
    => 54

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Mar 1, 2010
    #5
  6. On 01.03.2010 20:40, Reiichi Tyrael wrote:
    > David Springer wrote:
    >> Reiichi Tyrael,
    >>
    >> Is the range inclusive OR exclusive?
    >>
    >> Should n1 OR n2 be reurned sometimes or never?
    >>
    >>> number_to_guess = rand(n1..n2)

    > Inclusive!


    Sorry, I answered before seeing this email. In that case it should be

    irb(main):003:0> low, high = 12, 34
    => [12, 34]
    irb(main):004:0> r = low + rand(high + 1 - low)
    => 22

    You can also do something like this:

    irb(main):012:0> (low..high).to_a.sample
    => 21
    irb(main):013:0> (low..high).to_a.sample
    => 27

    Although in that case you should store the array somewhere for
    efficiency reasons.

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Mar 1, 2010
    #6
  7. It's work! Thank you David!
    But there is no way to do it like in my version "rand(n1..n2)"? It's
    more friendly that way XD

    --
    Posted via http://www.ruby-forum.com/.
    Reiichi Tyrael, Mar 1, 2010
    #7
  8. Reiichi Tyrael wrote:
    > It's work! Thank you David!
    > But there is no way to do it like in my version "rand(n1..n2)"? It's
    > more friendly that way XD


    #ruby 1.9
    puts (10..20).to_a.sample
    #ruby 1.8.6
    puts (10..20).to_a.sort_by{rand}.pop

    hth,

    Siep
    --
    Posted via http://www.ruby-forum.com/.
    Siep Korteling, Mar 1, 2010
    #8
  9. Reiichi Tyrael wrote:
    > It's work! Thank you David!
    > But there is no way to do it like in my version "rand(n1..n2)"? It's
    > more friendly that way XD


    Sure:

    def myrand(r=nil)
    if r.is_a?(Range)
    rand(r.last - r.first + (r.exclude_end? ? 0 : 1)) + r.first
    else
    rand(r)
    end
    end

    Or if you prefer:

    module Kernel
    alias :eek:rig_rand :rand
    def rand(r=nil)
    if r.is_a?(Range)
    orig_rand(r.last - r.first + (r.exclude_end? ? 0 : 1)) + r.first
    else
    orig_rand(r)
    end
    end
    end

    I suspect it's not implemented in Ruby because it's not obvious what
    rand(0.3..5.7) should do.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Mar 1, 2010
    #9
  10. Under Ruby 1.9:

    10.times.map{ Random.new.rand(20..30) } #=> [26, 26, 22, 20, 30,
    26, 23, 23, 25, 22]
    Run Paint Run Run, Mar 3, 2010
    #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. Replies:
    46
    Views:
    941
    Antoon Pardon
    Jul 25, 2006
  2. globalrev
    Replies:
    4
    Views:
    739
    Gabriel Genellina
    Apr 20, 2008
  3. Lambda
    Replies:
    2
    Views:
    380
    James Kanze
    Jul 16, 2008
  4. Ruby N00bie
    Replies:
    4
    Views:
    252
    William James
    Jan 6, 2007
  5. VK
    Replies:
    15
    Views:
    1,110
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page