Generating random numbers?

H

Harry Ohlsen

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).

Thanks in advance,

Harry O.
 
C

Cedric Foll

Harry said:
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).

Thanks in advance,

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].
 
B

Ben Giddings

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
 
B

Brandon D. Valentine

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
 
J

Josef 'Jupp' Schugt

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
 
P

Paul J. Sanchez

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

JS> Saluton! * Harry Ohlsen; 2003-07-28, 21:35 UTC:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top