The only thing it was being used for was for grabbing the element at that
index in an array.
I managed to figure out the problem though. I was using Kernel.srand(
Time.now.usec ) in a function that was executed previous to the one where I
was having a problem. Why this would result in duplicate sequential values,
I’m not sure. All I know is by taking srand out of a completely different
function, it fixed the problem.
In message “Re: Generating random numbers?” > on 03/07/30, “Orion Hunter” orion2480@hotmail.com writes:
My problem is that I am getting the same “random number” repeated in
sequence. For example, my code looks something like this
100.times{
rand = Kernel.rand( 50 )
… #do something with the random number
}
And I end up getting a sequence of random numbers like this:
5,5,5,5,5,5,5,5,89,89,89,89,89,2,2,2,78,78,78,78,44,44,44,44,44… etc
No one can answer for you unless you reveal “something with the random
number”. This is the first report of such randomness problem.
I suspect a bug in your code.
The only thing it was being used for was for grabbing the element at that
index in an array.
I managed to figure out the problem though. I was using Kernel.srand(
Time.now.usec ) in a function that was executed previous to the one where
I
was having a problem. Why this would result in duplicate sequential
values,
I’m not sure. All I know is by taking srand out of a completely different
function, it fixed the problem.
If you were reseeding over and over with the same
value, I can see where that might happen.
For example, this code (untested) I would expect
to produce such repetition:
100.times do
Kernel.srand(Time.now.usec)
puts rand(100)
end
That’s assuming the code was executing fast enough.
Cheers,
Hal
···
----- Original Message -----
From: “Orion Hunter” orion2480@hotmail.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, July 29, 2003 1:55 PM
Subject: Re: Generating random numbers?
The only thing it was being used for was for grabbing the element at that
index in an array.
I managed to figure out the problem though. I was using Kernel.srand(
Time.now.usec ) in a function that was executed previous to the
one where I was having a problem. Why this would result in duplicate
sequential values,
I’m not sure. All I know is by taking srand out of a completely
different function, it fixed the problem.
calling srand() more than once doesn’t make your numbers “more random”.
In message “Re: Generating random numbers?” on 03/07/30, “Orion Hunter” orion2480@hotmail.com writes:
I managed to figure out the problem though. I was using Kernel.srand(
Time.now.usec ) in a function that was executed previous to the one where I
was having a problem. Why this would result in duplicate sequential values,
I’m not sure. All I know is by taking srand out of a completely different
function, it fixed the problem.
You don’t need to call srand unless you want to reproduce random
number sequence. Besides, using time fraction as a random seed is
generally a bad idea.
This reminds me of a off-topic story involving srand(). Unfortunately
it has nothing to do with Ruby.
I was teaching an after hours C++ class at work. Part of the class
assignment was to write a Dice playing game. Students were to provide a
Player object that participated in a game framework. Being a C++ class,
I was stressing the difference between public and private and how the
framework prevented players from causually interfering with each other
and “cheating” in the game.
Of course, merely stressing this issue is enough motivation for some to
start looking for loopholes. One student discovered an interesting
“cheat”. His player object created a local copy of a dice object (he
didn’t have access to the actual Dice used by the framework). He then
rolled until he got a good score from his local dice. Before each roll,
he would record the value of the random seed. Just before he asked the
framework to roll the real dice object, he used srand to set the seed
that gave him a good score.
···
On Tue, 2003-07-29 at 15:40, Yukihiro Matsumoto wrote:
You don’t need to call srand unless you want to reproduce random
number sequence.