Re: lib - generating a list of random number

From: Robert K. [mailto:[email protected]]

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]

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

On 2006-11-14 19:25:09 +0100, Keynan P. [email protected]
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 …