# Lib - generating a list of random number

**URL:** <https://www.ruby-forum.com/t/lib-generating-a-list-of-random-number/75944>\
**Category:** Ruby\
**Created:** [November 14, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/lib-generating-a-list-of-random-number/75944 "2006-11-14T17:12:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [November 14, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/lib-generating-a-list-of-random-number/75944/1 "2006-11-14T17:12:56Z")

</div>

I try to generate an array of N random numbers in range between 2  
integers min and max  
but I don’t understand very well how to coordinate the sue of srand and  
rand…  
is there an already written function for that ? and btw is there any  
place to find such useful functions ?

thanks

joss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [November 14, 2006, 5:34pm UTC](https://www.ruby-forum.com/t/lib-generating-a-list-of-random-number/75944/2 "2006-11-14T17:34:58Z")

</div>

On Wed, 15 Nov 2006, Josselin wrote:

> 

harp:~ \> cat a.rb  
n, max, min = 42, 10, 6  
p Array.new(n).map{ rand((max - min) + 1) + min}

harp:~ \> ruby a.rb  
[6, 8, 8, 10, 9, 7, 6, 8, 9, 7, 10, 10, 7, 7, 9, 9, 6, 9, 10, 6, 8, 10,  
6, 9, 7, 8, 8, 10, 8, 9, 9, 9, 10, 7, 9, 6, 6, 8, 8, 10, 9, 7]

-a

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [November 14, 2006, 5:36pm UTC](https://www.ruby-forum.com/t/lib-generating-a-list-of-random-number/75944/3 "2006-11-14T17:36:59Z")

</div>

On 14.11.2006 17:09, Josselin wrote:

> I try to generate an array of N random numbers in range between 2  
> integers min and max  
> but I don’t understand very well how to coordinate the sue of srand and  
> rand…

I am not sure I understand you here. What does “sue” mean here?

> is there an already written function for that ? and btw is there any  
> place to find such useful functions ?

What about this?

> > def rand\_seq(count, min, max)  
> > (1…count).map { rand(max-min) + min }  
> > end  
> > =\> nil
> 
> > rand\_seq 10, -5, 5  
> > =\> [2, -4, -2, -1, -3, 1, 1, 1, -3, 0]

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [November 14, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/lib-generating-a-list-of-random-number/75944/4 "2006-11-14T19:11:27Z")

</div>

On 2006-11-14 17:30:31 +0100, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
said:

> What about this?  
> robert  
> sorry sue =\> usage… typing mistake…

thanks !!! that’s exactly what I searched for…  
I knew how to generate an array with an ‘upper limit only’ …

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [November 14, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/lib-generating-a-list-of-random-number/75944/5 "2006-11-14T18:21:14Z")

</div>

Josselin wrote:

> I try to generate an array of N random numbers in range between 2  
> integers min and max  
> but I don’t understand very well how to coordinate the sue of srand and  
> rand…

They are very easy to use, but first you have to decide what you want to  
do.  
What do you want to do? What role does “srand()” play? Be specific.

> is there an already written function for that ?

For what, specifically? The range of random numbers, or the use of  
“srand()”? If you always want the same sequence of random numbers, then  
there is one solution, but if you always want a different sequence,  
there  
is a different solution. Which do you want?

> and btw is there any  
> place to find such useful functions ?

There sure is:

> **[Ruby-Doc.org: Documenting the Ruby Language](https://ruby-doc.org/)**
>
> Fast, searchable Ruby documentation for core and standard libraries. Plus, links to tutorials, guides, books, and related sites.

By the way, when you ask a question like this, it is best to express  
your  
requirement as specifically as possible. You may have noticed that you  
got  
two different replies to your inquiry, each with sample code that  
provided  
differing interpretations of your request.

The solution to this problem is to leave no doubt about your  
requirement.  
For example, when you say “between two integers” do you mean:

a \< n \< b

or

a \<= n \<= b

or some third possibility?

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [November 14, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/lib-generating-a-list-of-random-number/75944/6 "2006-11-14T19:21:28Z")

</div>

On 2006-11-14 18:17:19 +0100, Paul L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> > is there an already written function for that ?  
> > [http://www.ruby-doc.org/](http://www.ruby-doc.org/)  
> > thanks
> 
> or  
> a \<= n \<= b
> 
> or some third possibility?

I apologize… I was looking for a result (how to generate a list of  
random integer numbers between a minimum and a maximum … included) …  
yes forgotten to specify including limits…

and at the same time trying to understand how to USE srand()and rand(),  
my standard Ruby manual is clearly written but there is no example for  
this function.  
I googled a lot but without any success regarding examples…  
my first thought was: srand() is useful in setting a limit to the  
generator… used by rand() so it could have been a clue to solve my  
problem…

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [November 14, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/lib-generating-a-list-of-random-number/75944/7 "2006-11-14T19:31:30Z")

</div>

On Wed, 15 Nov 2006, Josselin wrote:

> I googled a lot but without any success regarding examples…  
> my first thought was: srand() is useful in setting a limit to the  
> generator… used by rand() so it could have been a clue to solve my  
> problem…

Use [ruby-doc.org](http://ruby-doc.org)

[http://ruby-doc.org/core/classes/Kernel.html#M002012](http://ruby-doc.org/core/classes/Kernel.html#M002012)

Kirk H.

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [November 14, 2006, 9:11pm UTC](https://www.ruby-forum.com/t/lib-generating-a-list-of-random-number/75944/8 "2006-11-14T21:11:58Z")

</div>

Josselin wrote:

/ …

> and at the same time trying to understand how to USE srand()and rand(),  
> my standard Ruby manual is clearly written but there is no example for  
> this function.  
> I googled a lot but without any success regarding examples…  
> my first thought was: srand() is useful in setting a limit to the  
> generator… used by rand() so it could have been a clue to solve my  
> problem…

All except you haven’t yet said what the problem is. We could solve a  
stated  
problem, easily.

srand() sets a particular starting point for a pseudorandom sequence. If  
you  
use srand() and supply a fixed argument, you will get the same  
pseudorandom  
sequence on each run of your program:

* * *

#!/usr/bin/ruby -w

## 4.times do srand(1) 10.times do print "#{rand(16)} " end puts end

Notice about this program that it puts out the same sequence every time.  
Now  
comment out the “srand(1)” line and see the difference.
