Home » PLAYER'S HQ 1.13 » JA2 Complete Mods & Sequels » Stracciatella Project (Platform Independent JA2) » sgp/Random.cc
sgp/Random.cc[message #261036] Mon, 30 August 2010 13:01 Go to next message
mat69 is currently offline mat69

 
Messages:8
Registered:August 2010
UINT32 Random(UINT32 uiRange)

is supposed to "// Returns a pseudo-random integer between 0 and uiRange" though as far as I understood this is not true since you basically do
return rand() % uiRange

and that returns only numbers from 0 to uiRange - 1, instead you should do
return rand() % (uiRange + 1)


You should maybe think about including a better random generator, boost provides some good ones. The big advantage of boost would be that it would return the same result on all platforms, while rand heavily depends on RAND_MAX which is pretty low on Windows XP 32 bit (32767 !), no clue about Windows XP 64.

E.g.
UINT32 randNum()
{
  static boost::mt19937 rng(time(0));
  static boost::uniform_int<> dist(0, 2147483647);
  static boost::variate_generator > newRand(rng, high);
  return newRand();
}

UINT32 Random(UINT32 uiRange)
{
....
  return (uiRange ? (randNum() % (uiRange + 1)) : 0);
}

Report message to a moderator

Private
Re: sgp/Random.cc[message #261078] Mon, 30 August 2010 20:00 Go to previous messageGo to next message
public1983 is currently offline public1983

 
Messages:125
Registered:February 2006

Apart from your suggestion, is that not just a bad comment? Can you name any bug caused by the random number generation in the half open interval? Does it screw the imp-generator or so?

Report message to a moderator

Sergeant
Re: sgp/Random.cc[message #261105] Mon, 30 August 2010 23:14 Go to previous messageGo to next message
mgl is currently offline mgl

 
Messages:255
Registered:December 2007
Location: France
In all the patches I have written I have expected "Random(n)" to return something in the range [0, n - 1]. I think it should return either this or [1, n], but not [0, n]. I wrote yesterday in a thread about bugs in the IMP creation interface that "Random()" is like a dice: "Random(6)" creates a dice with 6 faces numbered 0 to 5. What would you do of the seventh face if it returned 0 to 6 ? And, more important, would you even expect that a line of code which naturally reads as a request to create 6 possibilities creates truly seven of them instead ?

In the original code, the calls to "Random()" are sometimes already off by one (because of calls to "Random(range + 1)"). They would be off by two if the range was increased.

The comment in the Random() function is confusing and was probably confusing for the original coders too. And there may be a minor problem with the "Chance()" function, which uses "Random()". A comment says that "Chance(100)" should always be 100% but it is possible that it is 99% only. I don't know, I have never read the code carefully.

And for the alternate random generator, since the function is used very often, if Tron didn't already do it, he probably won't.

I think the code was shared or taken from the coders of the game "Wizardry VII". It may match the needs of Wizardry, not JA2.

Report message to a moderator

Master Sergeant
Re: sgp/Random.cc[message #261106] Mon, 30 August 2010 23:17 Go to previous messageGo to next message
bbun is currently offline bbun

 
Messages:74
Registered:April 2004
Location: Amsterdam
It's stupid, but causes no bug afaik.

Talking about stupid, I've always laughed at them creating the Chance() function, but rarely using it. Instead they keep doing a manual rand(100) < c ....

Report message to a moderator

Corporal
Re: sgp/Random.cc[message #261114] Tue, 31 August 2010 01:01 Go to previous messageGo to next message
mat69 is currently offline mat69

 
Messages:8
Registered:August 2010
So basically that means that the comment is wrong? No clue if a bug is caused by this or not, I just stumbled over the code itself. Wink

Actually they use Random and PreRandom most of the times and rarely use rand directly.

Yes, Chance(100) is always true, since Random(100) returns values ranging from 0 to 99.

Btw. I have no clue if just having 256 pregenerated random figures -- they get reused -- influences the game a bad way, I mean random stuff is hard (impossible?) to debug, but it would be easy to generated another 256 if all of them have been used already:
UINT32 PreRandom( UINT32 uiRange )
{
....
        if( guiPreRandomIndex >= (UINT32)MAX_PREGENERATED_NUMS )
//        guiPreRandomIndex = 0;//remove this line
                InitializeRandom();//add that
....

Report message to a moderator

Private
Re: sgp/Random.cc[message #261138] Tue, 31 August 2010 09:25 Go to previous message
public1983 is currently offline public1983

 
Messages:125
Registered:February 2006

I have found pieces of code, where the authors were apparently aware of the functionality of Random.

Item_Statistics.cc with a random number between 20 and 100:
		i = 20 + Random( 81 );


GameInit.cc
		switch (Random(4))
		{
			case 0:
				p.sSectorX = 15;
				p.sSectorY = MAP_ROW_B;
				p.bSectorZ = 0;
				break;
			case 1:
				p.sSectorX = 14;
				p.sSectorY = MAP_ROW_E;
				p.bSectorZ = 0;
				break;
			case 2:
				p.sSectorX = 12;
				p.sSectorY = MAP_ROW_D;
				p.bSectorZ = 0;
				break;
			case 3:
				p.sSectorX = 16;
				p.sSectorY = MAP_ROW_C;
				p.bSectorZ = 0;
				break;
		}


It's worth investigating though, whether this source of errors has creeped in somewhere.

Mercs.cc with a 49 % chance?
		if( Random( 100 ) > 50 )
			StartSpeckTalking( SPECK_QUOTE_GENERIC_THANKS_FOR_HIRING_MERCS_1 );
		else
			StartSpeckTalking( SPECK_QUOTE_GENERIC_THANKS_FOR_HIRING_MERCS_2 );


[Updated on: Tue, 31 August 2010 13:40] by Moderator

Report message to a moderator

Sergeant
Previous Topic: IMP test attitude and traits
Next Topic: Stracciatella Ramblings
Goto Forum:
  


Current Time: Wed May 15 05:30:08 GMT+3 2024

Total time taken to generate the page: 0.01323 seconds