Random number generator

General Programming help in C, C++ or ASM, Compiling / Debugging, and R3000A Central Processing Unit (CPU) information
Post Reply
User avatar
mentor
Interested PSXDEV User
Interested PSXDEV User
Posts: 5
Joined: Jan 04, 2016
Location: Poland

Random number generator

Post by mentor » January 4th, 2016, 1:32 am

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?

User avatar
Shadow
Verified
Admin / PSXDEV
Admin / PSXDEV
Posts: 2670
Joined: Dec 31, 2012
PlayStation Model: H2000/5502
Discord: Shadow^PSXDEV

Post by Shadow » January 4th, 2016, 1:40 am

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?
Use the system root counter as a randomiser base and parse it through a custom algorithm (function).

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;
}
or

Code: Select all

///////////////////////////////////////////////////////////
// Random Number Generator (local)
///////////////////////////////////////////////////////////
int Random(int max)
{
	int RAN = 0;
	RAN = (rand()%max);
	return RAN;
}
All you need to do is make it use an algo based off of the root counter.
'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.

User avatar
mentor
Interested PSXDEV User
Interested PSXDEV User
Posts: 5
Joined: Jan 04, 2016
Location: Poland

Post by mentor » January 4th, 2016, 3:51 am

Thanks for your answer. If I understand you correctly, I need to set srand() parameter to GetRCnt(RCntCNT1)), like this:

Code: Select all

srand(GetRCnt(RCntCNT1));
But before I can use GetRCnt() function I need to set RCntCNT1 mode to RCntMdNOINTR and this can be done by calling SetRCnt:

Code: Select all

SetRCnt(RCntCNT1,0,RCntMdNOINTR);
I don't really know what second parameter here should be. 0? Something else? Is this way of setting seed even correct?

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest