# Re: lib - generating a list of random number

**URL:** <https://www.ruby-forum.com/t/re-lib-generating-a-list-of-random-number/75960>\
**Category:** Ruby\
**Created:** [November 14, 2006, 6:55pm UTC](https://www.ruby-forum.com/t/re-lib-generating-a-list-of-random-number/75960 "2006-11-14T18:55:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 14, 2006, 6:55pm UTC](https://www.ruby-forum.com/t/re-lib-generating-a-list-of-random-number/75960/1 "2006-11-14T18:55:24Z")

</div>

From: Robert K. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> On 14.11.2006 17:09, Josselin wrote:
> 
> > 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?

I think Josselin meant “use”.

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

I think you have a fencepost error there…  
irb(main):017:0\> r = rand\_seq( 1\_000\_000, -10, 10 )  
irb(main):018:0\> r.min  
=\> -10  
irb(main):019:0\> r.max  
=\> 9  
…though it depends if the OP wanted min/max to be inclusive or  
exclusive.

Also, it depends on if the OP wanted integers only or floats. If  
floating point numbers are desired, here’s an alternative:

def rand\_seq(count, min, max)  
(1…count).map { rand \* (max-min) + min }  
end

r = rand\_seq( 10\_000\_000, -10, 10 )  
p r.min, r.max  
#=\> -9.99999704788504  
#=\> 9.99999886739173

p r[0…10]  
#=\> [6.89334712606148, -3.51560170870142, 3.62144692732178,  
9.29043168269461, -8.24044290511051, -6.06086914781006,  
-6.78123809802907, 7.52184969087783, -2.21712322878881,  
-0.845330115529201, -2.82092973043312]

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 14, 2006, 7:25pm UTC](https://www.ruby-forum.com/t/re-lib-generating-a-list-of-random-number/75960/2 "2006-11-14T19:25:06Z")

</div>

srand refers to seed rand  
rand is not generating a random number it is just picing a number from a  
list of huge size srand says start from this position so passing in the  
time.now before you start looping you will get more random results

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 15, 2006, 7:17am UTC](https://www.ruby-forum.com/t/re-lib-generating-a-list-of-random-number/75960/3 "2006-11-15T07:17:00Z")

</div>

On 2006-11-14 19:25:09 +0100, Keynan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
said:

> srand refers to seed rand  
> rand is not generating a random number it is just picing a number from  
> a list of huge size srand says start from this position so passing in  
> the time.now before you start looping you will get more random results

thanks a lot ! got it …
