| sgp/Random.cc[message #261036]
|
Mon, 30 August 2010 13:01
|
|
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
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 #261105]
|
Mon, 30 August 2010 23:14 
|
|
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
|
|
|
|
|
|
|
|
|
| Re: sgp/Random.cc[message #261138]
|
Tue, 31 August 2010 09:25
|
|
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:
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
|
|
|
|