Random number generator
Random number generator
I need to generate a random (really random, not pseudo-random) number at the very beginning of the program. I need to initialize the position of a certain sprite and this position has to be different each time the game starts. rand() itself generates the same position every time I start the game, so I need to seed it using srand(). On Windows I would use time(NULL) as a parameter here, but PSY-Q doesn't provide us with time() function (which is fully understandable). I searched the docs for some replacement of time(), but I haven't found anything. It seems like there's no way I could get PS1 system time or anything similar to it. Psymake doesn't recognize rdtsc()... And so I'm actually out of ideas. srand() needs some initial starting point as a seed and this seed also needs to be different each time. Therefore, how to seed srand(), so rand() would generate random number, different each time the program starts?
-
Administrator Verified
- Admin / PSXDEV
- Posts: 2691
- Joined: Dec 31, 2012
- I am a: Shadow
- PlayStation Model: H2000/5502
Use the system root counter as a randomiser base and parse it through a custom algorithm (function).mentor wrote:I need to generate a random (really random, not pseudo-random) number at the very beginning of the program. I need to initialize the position of a certain sprite and this position has to be different each time the game starts. rand() itself generates the same position every time I start the game, so I need to seed it using srand(). On Windows I would use time(NULL) as a parameter here, but PSY-Q doesn't provide us with time() function (which is fully understandable). I searched the docs for some replacement of time(), but I haven't found anything. It seems like there's no way I could get PS1 system time or anything similar to it. Psymake doesn't recognize rdtsc()... And so I'm actually out of ideas. srand() needs some initial starting point as a seed and this seed also needs to be different each time. Therefore, how to seed srand(), so rand() would generate random number, different each time the program starts?
Here is a small example of a random function:
Code: Select all
///////////////////////////////////////////////////////////
// Random Number Generator (global)
///////////////////////////////////////////////////////////
int Random(int max)
{
RAN = (rand()%max);
return 0;
}
Code: Select all
///////////////////////////////////////////////////////////
// Random Number Generator (local)
///////////////////////////////////////////////////////////
int Random(int max)
{
int RAN = 0;
RAN = (rand()%max);
return RAN;
}
'max' is the maximum amount you would like to return. So if you parse 10, you will get numbers 1 through 10.
Development Console: SCPH-5502 with 8MB RAM, MM3 Modchip, PAL 60 Colour Modification (for NTSC), PSIO Switch Board, DB-9 breakout headers for both RGB and Serial output and an Xplorer with CAETLA 0.34.
PlayStation Development PC: Windows 98 SE, Pentium 3 at 400MHz, 128MB SDRAM, DTL-H2000, DTL-H2010, DTL-H201A, DTL-S2020 (with 4GB SCSI-2 HDD), 21" Sony G420, CD-R burner, 3.25" and 5.25" Floppy Diskette Drives, ZIP 100 Diskette Drive and an IBM Model M keyboard.
PlayStation Development PC: Windows 98 SE, Pentium 3 at 400MHz, 128MB SDRAM, DTL-H2000, DTL-H2010, DTL-H201A, DTL-S2020 (with 4GB SCSI-2 HDD), 21" Sony G420, CD-R burner, 3.25" and 5.25" Floppy Diskette Drives, ZIP 100 Diskette Drive and an IBM Model M keyboard.
Thanks for your answer. If I understand you correctly, I need to set srand() parameter to GetRCnt(RCntCNT1)), like this:
But before I can use GetRCnt() function I need to set RCntCNT1 mode to RCntMdNOINTR and this can be done by calling SetRCnt:
I don't really know what second parameter here should be. 0? Something else? Is this way of setting seed even correct?
Code: Select all
srand(GetRCnt(RCntCNT1));
Code: Select all
SetRCnt(RCntCNT1,0,RCntMdNOINTR);
Who is online
Users browsing this forum: No registered users and 4 guests