Page 1 of 2

SRAND( ) / RAND( ) issue

PostPosted: Sep 25, 2002 @ 4:09pm
by ppcStudios
Does GapiDraw happen to reseed the pseudorandom number generator with SRAND(val) on a periodic basis? The reason I'm asking is that unless I reseed prior to making a RAND( ) call, I get the same random number sequence back all the time. If I make the SRAND(GetTickCount()) call at the start of the application (this is using the CMyApplication framework), it has no effect on the return value of RAND( ). Here's an example :

Using the CMyApplication framework (I used the Simple sample program supplied with 1.04) place

DWORD dwSeed = GetTickCount();
srand(dwSeed);

just before "return simple.Run();" in WinMain( ).

This will seed the random number generator. You should only have to do this once at the start of an application. Multiple SRAND( ) calls are highly discouraged.

Now, in ProcessNextFrame( ), place this line at the top of the method

int r = (rand() % 10) + 1;

This will generate a random number from 1 to 10. Pretty simple stuff...

Launch the debugger and set a breakpoint on the rand call. Make note of the value that is returned and restart the debugger several times. You'll find that the random number is the same everytime.

Now, copy the srand code just before the rand line and run the debugger... random numbers as expected.

This says to me that srand is being reseeded with a value by GapiDraw every frame. I don't see this problem with a simple application outside the CMyApplication framework. Is there something I'm missing here?

Thanks for any insight on this problem... -- GR

PostPosted: Sep 25, 2002 @ 4:58pm
by ppcStudios

Rand

PostPosted: Sep 25, 2002 @ 5:18pm
by Guest

PostPosted: Sep 25, 2002 @ 5:24pm
by ppcStudios

PostPosted: Sep 25, 2002 @ 5:37pm
by ppcStudios

PostPosted: Sep 25, 2002 @ 6:05pm
by ppcStudios
As a followup...

I modified the rand_num class from the XKobo source to fit my needs, and am using it to generate random numbers. This is now working correctly since it doesn't rely on srand/rand to create the values. I think that this rand issue _could_ be resolved in GapiDraw if the main thread initialized the seed value with srand(GetTickCounter()) [or srand((unsigned)time(NULL))] before creating any other threads. The seed value _should_ propogate through the other threads and provide a base for rand to work from.

-- GR

SRAND( ) revisited

PostPosted: Oct 10, 2002 @ 9:18pm
by ppcStudios
I've been working using the XKobo random number generator to finish up work I'm doing on a Pachisi game, but after using it for several weeks I'm not happy with its performance at all. Random numbers are simply sequential (in a fashion) and its guaranteed you will _never_ get a duplicate result in calls to the method (eg never roll a double for example on 2 dice). I can reseed the the generator every time I want a random number and that works fine on desktop PCs, but not at all on pocket PCs. So, I'm now looking at other alternatives for random numbers.

What is everyone here using to generate random numbers in the GapiDraw framework?

-- GR

PostPosted: Oct 10, 2002 @ 10:37pm
by Sam Nova
I would suggest you check the Mersenne Twister methode for generating random numbers. Just search for it on Google and you will find lot of resources. (If you want I can send you mine..).

I have sucessfully used it on the GBA game I did some time back.

best,

Sam

PostPosted: Oct 11, 2002 @ 3:51am
by ppcStudios

PostPosted: Oct 11, 2002 @ 10:28pm
by Kzinti

PostPosted: Oct 11, 2002 @ 11:44pm
by Johan

PostPosted: Oct 12, 2002 @ 1:49am
by Kzinti

PostPosted: Oct 12, 2002 @ 3:08am
by ppcStudios

PostPosted: Oct 12, 2002 @ 3:30am
by Kzinti

PostPosted: Oct 12, 2002 @ 6:37am
by Kzinti
I re-read the thread and just found out that threads are part of GapiDraw. I was unaware of that and though ppcStudios was creating threads himself.

Johan, my comments may have carried the wrong tone because of that, and I sincerely apologize. It was in no way my intention to challenge your design.

I still believe threads sound nice from a theorical point of view. But my practical experience with them have teached me to stay away as much as possible. Nevertheless, I would definitely use them for low-level networking code and for a windowed application that needs to run interactively.