# Generating random numbers?

**URL:** <https://rubytalk.org/t/generating-random-numbers/7068>\
**Category:** ruby-talk\
**Created:** [28 July 2003 21:19 UTC](https://rubytalk.org/t/generating-random-numbers/7068 "2003-07-28T21:19:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harry\_Ohlsen2](https://avatars.discourse-cdn.com/v4/letter/h/958977/32.png) [@Harry\_Ohlsen2](https://rubytalk.org/u/Harry_Ohlsen2)\
**Post date:** [28 July 2003 21:19 UTC](https://rubytalk.org/t/generating-random-numbers/7068/1 "2003-07-28T21:19:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ben\_Giddings](https://avatars.discourse-cdn.com/v4/letter/b/b19c9b/32.png) [@Ben\_Giddings](https://rubytalk.org/u/Ben_Giddings)\
**Post date:** [28 July 2003 21:27 UTC](https://rubytalk.org/t/generating-random-numbers/7068/2 "2003-07-28T21:27:31Z")

</div>

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](http://www.rubycentral.com/book/ref_m_kernel.html#Kernel.rand)

Ben

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

---

<div class="post-metadata">

**Author:** ![Warren\_Brown3](https://avatars.discourse-cdn.com/v4/letter/w/9d8465/32.png) [@Warren\_Brown3](https://rubytalk.org/u/Warren_Brown3)\
**Post date:** [28 July 2003 21:28 UTC](https://rubytalk.org/t/generating-random-numbers/7068/3 "2003-07-28T21:28:31Z")

</div>

Harry,

> Can someone remind me of the name for the  
> class/method for generating random numbers?

```
Would you believe....... rand?

Kernel#rand takes one optional parameter that if 0 (the default) returns

```

a Float x such that 0 \<= x \< 1, and if not zero, returns an Integer x such  
that 0 \<= x \<= parameter. Use Kernel#srand to seed the random number  
generator.

```
I hope this helps!

- Warren Brown

```

---

<div class="post-metadata">

**Author:** ![Brandon\_D\_Valentine](https://avatars.discourse-cdn.com/v4/letter/b/a6a055/32.png) [@Brandon\_D\_Valentine](https://rubytalk.org/u/Brandon_D_Valentine)\
**Post date:** [28 July 2003 21:31 UTC](https://rubytalk.org/t/generating-random-numbers/7068/4 "2003-07-28T21:31:34Z")

</div>

Kernel::rand

Seed it via:

Kernel::srand

HTH,

Brandon D. Valentine

> **···**
>
> 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).
> 
> –  
> [brandon@dvalentine.com](mailto:brandon@dvalentine.com)[http://www.geekpunk.net](http://www.geekpunk.net)  
> Pseudo-Random Googlism: texas is the reason do you know who you are?

---

<div class="post-metadata">

**Author:** ![Cedric\_Foll1](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@Cedric\_Foll1](https://rubytalk.org/u/Cedric_Foll1)\
**Post date:** [30 July 2003 02:24 UTC](https://rubytalk.org/t/generating-random-numbers/7068/5 "2003-07-30T02:24:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Josef\_Jupp\_SCHUGT](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@Josef\_Jupp\_SCHUGT](https://rubytalk.org/u/Josef_Jupp_SCHUGT)\
**Post date:** [30 July 2003 20:28 UTC](https://rubytalk.org/t/generating-random-numbers/7068/6 "2003-07-30T20:28:44Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Harry\_Ohlsen2](https://avatars.discourse-cdn.com/v4/letter/h/958977/32.png) [@Harry\_Ohlsen2](https://rubytalk.org/u/Harry_Ohlsen2)\
**Post date:** [28 July 2003 21:43 UTC](https://rubytalk.org/t/generating-random-numbers/7068/7 "2003-07-28T21:43:38Z")

</div>

Thanks!

> **···**
>
> On Tue, 29 Jul 2003 07:28, Warren Brown wrote:
> 
> > Harry,
> > 
> > > Can someone remind me of the name for the  
> > > class/method for generating random numbers?
> > 
> > ```
> > Would you believe....... rand?
> > 
> > Kernel#rand takes one optional parameter that if 0 (the default)
> > 
> > ```
> > 
> > returns a Float x such that 0 \<= x \< 1, and if not zero, returns an Integer  
> > x such that 0 \<= x \<= parameter. Use Kernel#srand to seed the random  
> > number generator.
> > 
> > ```
> > I hope this helps!
> > 
> > ```

---

<div class="post-metadata">

**Author:** ![Harry\_Ohlsen2](https://avatars.discourse-cdn.com/v4/letter/h/958977/32.png) [@Harry\_Ohlsen2](https://rubytalk.org/u/Harry_Ohlsen2)\
**Post date:** [30 July 2003 20:59 UTC](https://rubytalk.org/t/generating-random-numbers/7068/8 "2003-07-30T20:59:10Z")

</div>

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

That’s a good question. I’m no statistics guru, so I probably don’t really  
know :-).

However, I’m guessing that uniformly distributed is probably fine, which I  
think is what rand() tries to generate.

What I’m using them for is to try to find a generator in Z\_n. I plan to do  
some experimentation to see whether doing it that way (there’s a little more  
to the approach, but it’s basically random) compared to using brute force  
(ie, checking the order of 2, 3, … until we find one that’s phi(n)).

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

Thanks. It might be good for my experiment. I’ll take a look.

Harry O.

> **···**
>
> On Thu, 31 Jul 2003 06:28, Josef ‘Jupp’ Schugt wrote:

---

<div class="post-metadata">

**Author:** ![Paul9](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@Paul9](https://rubytalk.org/u/Paul9)\
**Post date:** [31 July 2003 17:42 UTC](https://rubytalk.org/t/generating-random-numbers/7068/9 "2003-07-31T17:42:15Z")

</div>

> 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

```

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
