# Generating random numbers?

Discussion in 'Ruby' started by Harry Ohlsen, Jul 28, 2003.

1. ### Harry OhlsenGuest

Can someone remind me of the name for the class/method for generating random
numbers? I did a quick grep through the library and a google, but all I
found was a project that plans to improve the random number generator(s).

Harry O.

Harry Ohlsen, Jul 28, 2003

2. ### Cedric FollGuest

Harry Ohlsen wrote:

> Can someone remind me of the name for the class/method for generating random
> numbers? I did a quick grep through the library and a google, but all I
> found was a project that plans to improve the random number generator(s).
>
>
> Harry O.
>
>

oban:~# irb
irb(main):001:0> rand(10)
=> 8

You have the rand method.
rand(n) return a number in [1..n-1].

Cedric Foll, Jul 28, 2003

3. ### Ben GiddingsGuest

On Mon July 28 2003 5:19 pm, Harry Ohlsen wrote:
> Can someone remind me of the name for the class/method for generating random
> numbers? I did a quick grep through the library and a google, but all I
> found was a project that plans to improve the random number generator(s).

Kernel::rand

irb(main):012:0> rand
=> 0.1986612797
irb(main):013:0> rand(5)
=> 4

Use the first for a floating point random between 0 and 1, the second for an
integer rand between 0 and the number.

http://www.rubycentral.com/book/ref_m_kernel.html#Kernel.rand

Ben

Ben Giddings, Jul 28, 2003
4. ### Brandon D. ValentineGuest

On Tue, Jul 29, 2003 at 06:19:58AM +0900, Harry Ohlsen wrote:
> Can someone remind me of the name for the class/method for generating random
> numbers? I did a quick grep through the library and a google, but all I
> found was a project that plans to improve the random number generator(s).

Kernel::rand

Seed it via:

Kernel::srand

HTH,

Brandon D. Valentine
--
http://www.geekpunk.net
Pseudo-Random Googlism: texas is the reason do you know who you are?

Brandon D. Valentine, Jul 28, 2003
5. ### Josef 'Jupp' SchugtGuest

Saluton!

* Harry Ohlsen; 2003-07-28, 21:35 UTC:
> Can someone remind me of the name for the class/method for
> generating random numbers?

Nobody did ask this so far: What kind of random numbers do you need
and for what purpose.

Ruby's RNG may be nice for Monte Carlo simulations (because it has a
very large period) but it is NOT SECURE for CRYPTOGRAPHY as it is.
You may wish to visit

http://www.math.keio.ac.jp/matumoto/emt.html

Gis,

Josef 'Jupp' Schugt
--
N'attribuez jamais à la malice ce que l'incompétence explique !
-- Napoléon

Josef 'Jupp' Schugt, Jul 30, 2003
6. ### Paul J. SanchezGuest

>>>>> "JS" == Josef Schugt <Josef> writes:

JS> Saluton! * Harry Ohlsen; 2003-07-28, 21:35 UTC:
>> Can someone remind me of the name for the class/method for
>> generating random numbers?

JS> Nobody did ask this so far: What kind of random numbers do you
JS> need and for what purpose.

JS> Ruby's RNG may be nice for Monte Carlo simulations (because it
JS> has a very large period) but it is NOT SECURE for CRYPTOGRAPHY
JS> as it is. You may wish to visit

JS> http://www.math.keio.ac.jp/matumoto/emt.html

JS> Gis,

JS> Josef 'Jupp' Schugt -- N'attribuez jamais à la malice ce que
JS> l'incompétence explique ! -- Napoléon

Actually, Ruby's RNG isn't so great for Monte Carlo or discrete event
simulations. I'd only use it for games. The problem isn't the
quality of the generator (I think Matz is using the Mersenne twister
algorithm). It's that "randomness" is obtained by a method rather
than a Random class which can instantiate Random objects. If there
were separate random objects, each of which maintained its own seed
state, it would be much easier to create designed experiments with
common or antithetic random variates which you could exploit for
variance reduction. Having a method restricts you to a single stream
of randomness unless you're willing to go through contortions.

--paul

Paul J. Sanchez, Jul 31, 2003