Re: random value problem
Re: random value problem
- Subject: Re: random value problem
- From: Greg Titus <email@hidden>
- Date: Sun, 21 Apr 2002 23:11:03 -0700
On Thursday, April 18, 2002, at 01:09 PM, Mike Brinkman wrote:
The one problem I'm experiencing though is that if I hit the return key
quickly, the program doesn't generate new random numbers. If I wait
half a
second between hitting the key, new numbers get generated. I know that
it
isn't creating new numbers, because I've got a counter that counts how
many
times somebody has played.
I call srandom(time(NULL)) at the top of my method that receives the
button/enter key action. Anybody have any ideas?
Hi Mike,
Your problem is that you are misusing the srandom() function.
srandom is for random number setup. It sets the 'seed' value that all
future random numbers are generated from. Your line
"srandom(time(NULL))" takes the current time, in seconds, and feeds it
in as the 'seed' value for future random numbers.
When you do this twice during the same second, you are essentially
resetting the random number generator to exactly the same state it had
before. So you are generating numbers, they just end up being exactly
the same numbers as they were before.
Move the "srandom(time(NULL))" line out of your action method. It only
needs to be called once during the run of your program, before the first
use of random(). -applicationDidFinishLaunching: would be a good place.
Hope this helps,
--Greg
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.