Forum: Ruby generating negative random numbers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6b2144362fffd4f71cca755d4045846f?d=identicon&s=25 Josselin (Guest)
on 2007-01-30 08:11
(Received via mailing list)
I know how to generate positive random numbers, but is it possible to
generate a random number in a range such  [-9..+9]   ?
tfyh

joss
47e2d5513caa1db234f3481d2d528838?d=identicon&s=25 Gerald Ebberink (Guest)
on 2007-01-30 08:16
(Received via mailing list)
Rand(18)-9

Gerald Ebberink
Laser Technician

NCLR B.V.
PO box 2662
Enschede 7500CR
The Netherlands
  Work: 31 53 4891110
Direct: 31 53 4893961
Fax: 31 53 4891102
Email: g.h.p.ebberink@nclr.nl

http://www.linkedin.com/in/geraldebberink




    Want a signature like this?
04ac594e52733356cec21d8ecf99c0b1?d=identicon&s=25 Tim Bray (Guest)
on 2007-01-30 08:21
(Received via mailing list)
On Jan 29, 2007, at 11:10 PM, Josselin wrote:

> I know how to generate positive random numbers, but is it possible
> to generate a random number in a range such  [-9..+9]   ?

irb(main):001:0> def rrand(x)
irb(main):002:1> rand(2*x) - x
irb(main):003:1> end
=> nil
irb(main):004:0> rrand 9
=> -7
irb(main):005:0> rrand 9
=> -8
irb(main):006:0> rrand 9
=> 5
irb(main):007:0> rrand 9
=> -8
6b2144362fffd4f71cca755d4045846f?d=identicon&s=25 Josselin (Guest)
on 2007-01-30 08:21
(Received via mailing list)
On 2007-01-30 08:14:39 +0100, "Gerald Ebberink" <g.h.p.ebberink@nclr.nl>
said:

> Direct: 31 53 4893961
> -----Original Message-----
> From: Josselin [mailto:josselin@wanadoo.fr]
> Sent: dinsdag 30 januari 2007 8:10
> To: ruby-talk ML
> Subject: generating negative random numbers
>
> I know how to generate positive random numbers, but is it possible to
> generate a random number in a range such  [-9..+9]   ?
> tfyh
>
> joss

thanks ... I'd never think about it !
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2007-01-30 08:24
(Received via mailing list)
On 30/01/07, Josselin <josselin@wanadoo.fr> wrote:
> I know how to generate positive random numbers, but is it possible to
> generate a random number in a range such  [-9..+9]   ?
> tfyh
>
> joss
>
>
>

rand(19) - 9

Farrel
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2007-01-30 08:53
(Received via mailing list)
On 1/30/07, Josselin <josselin@wanadoo.fr> wrote:
> I know how to generate positive random numbers, but is it possible to
> generate a random number in a range such  [-9..+9]   ?
> tfyh

class Range
  def rand
    Kernel::rand(last - first + 1) + first
  end
end

(-9..9).rand

martin
6b2144362fffd4f71cca755d4045846f?d=identicon&s=25 Josselin (Guest)
on 2007-02-01 09:10
(Received via mailing list)
On 2007-01-30 08:05:18 +0100, Josselin <josselin@wanadoo.fr> said:

> I know how to generate positive random numbers, but is it possible to
> generate a random number in a range such  [-9..+9]   ?
> tfyh
>
> joss

thanks to all of u, got it, but why there is no 'standard' function in
the lib ...
E4d8c6e0e9e5ca6ab51b4b1937586e6f?d=identicon&s=25 Christoffer Lernö (Guest)
on 2007-02-27 14:13
(Received via mailing list)
On Jan 30, 2007, at 08:52 , Martin DeMello wrote:

>
> (-9..9).rand

(-9...10).rand won't give the expected answer. I think the below
should fix the problem when the last entry in the range is excluded:

class Range
  def rand
    return first if exclude_end? && last == first
    Kernel::rand(last - first + (exclude_end? ? 0 : 1)) + first
  end
end
This topic is locked and can not be replied to.