This site is no longer active and is available for archival purposes only. Registration and login is disabled.

SRAND( ) / RAND( ) issue


SRAND( ) / RAND( ) issue

Postby ppcStudios » Sep 25, 2002 @ 4:09pm

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
User avatar
ppcStudios
pm Insider
 
Posts: 744
Joined: Aug 23, 2002 @ 3:53pm
Location: Canfield, Ohio


Postby ppcStudios » Sep 25, 2002 @ 4:58pm

User avatar
ppcStudios
pm Insider
 
Posts: 744
Joined: Aug 23, 2002 @ 3:53pm
Location: Canfield, Ohio


Rand

Postby Guest » Sep 25, 2002 @ 5:18pm

Guest
 


Postby ppcStudios » Sep 25, 2002 @ 5:24pm

User avatar
ppcStudios
pm Insider
 
Posts: 744
Joined: Aug 23, 2002 @ 3:53pm
Location: Canfield, Ohio


Postby ppcStudios » Sep 25, 2002 @ 5:37pm

User avatar
ppcStudios
pm Insider
 
Posts: 744
Joined: Aug 23, 2002 @ 3:53pm
Location: Canfield, Ohio


Postby ppcStudios » Sep 25, 2002 @ 6:05pm

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
User avatar
ppcStudios
pm Insider
 
Posts: 744
Joined: Aug 23, 2002 @ 3:53pm
Location: Canfield, Ohio


SRAND( ) revisited

Postby ppcStudios » Oct 10, 2002 @ 9:18pm

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
User avatar
ppcStudios
pm Insider
 
Posts: 744
Joined: Aug 23, 2002 @ 3:53pm
Location: Canfield, Ohio


Postby Sam Nova » Oct 10, 2002 @ 10:37pm

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
Sam Nova
pm Member
 
Posts: 34
Joined: Mar 29, 2002 @ 11:02pm
Location: Switzerland


Postby ppcStudios » Oct 11, 2002 @ 3:51am

User avatar
ppcStudios
pm Insider
 
Posts: 744
Joined: Aug 23, 2002 @ 3:53pm
Location: Canfield, Ohio


Postby Kzinti » Oct 11, 2002 @ 10:28pm

Kzinti
pm Member
 
Posts: 3238
Joined: Jan 13, 2002 @ 5:23am


Postby Johan » Oct 11, 2002 @ 11:44pm

User avatar
Johan
pm Member
 
Posts: 1843
Joined: Jan 12, 2002 @ 12:38pm
Location: Sweden


Postby Kzinti » Oct 12, 2002 @ 1:49am

Kzinti
pm Member
 
Posts: 3238
Joined: Jan 13, 2002 @ 5:23am


Postby ppcStudios » Oct 12, 2002 @ 3:08am

User avatar
ppcStudios
pm Insider
 
Posts: 744
Joined: Aug 23, 2002 @ 3:53pm
Location: Canfield, Ohio


Postby Kzinti » Oct 12, 2002 @ 3:30am

Kzinti
pm Member
 
Posts: 3238
Joined: Jan 13, 2002 @ 5:23am


Postby Kzinti » Oct 12, 2002 @ 6:37am

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.
Kzinti
pm Member
 
Posts: 3238
Joined: Jan 13, 2002 @ 5:23am


Next

Return to GapiDraw


Sort


Forum Description

The Cross-platform Graphics SDK for Palms, Pocket PCs, Symbian Devices, and Stationary PCs.

Moderators:

sponge, Johan

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

cron