New option NEW_RANDOM (default TRUE) enables new code for random number generation.

Fixed PreRandom(1) bug in AddPlacementToWorld.
AddPossiblePendingEnemiesToBattle: use Random(4) instead of GetRndNum(3) for random insertion code.

git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@8747 3b4a5df2-a311-0410-b5c6-a8a6f20db521
This commit is contained in:
Sevenfm
2020-02-09 18:35:04 +00:00
parent 90db9fa346
commit 6005382920
6 changed files with 52 additions and 15 deletions
+21 -2
View File
@@ -40,6 +40,27 @@ UINT32 MPPreRandom( UINT32 uiRange )
UINT32 guiPreRandomIndex;
UINT32 guiPreRandomNums[MAX_PREGENERATED_NUMS];
#include <random>
std::random_device gRandomDevice; // only used once to initialize (seed) engine
std::mt19937 gRandomNumberGenerator(gRandomDevice()); // random-number engine used (Mersenne-Twister in this case)
UINT32 NewRandom(UINT32 max)
{
if (is_networked && is_client)
return MPPreRandom(max);
if (max <= 1)
return 0;
UINT32 min = 0;
std::uniform_int_distribution<int> uni(min, max - 1); // guaranteed unbiased
auto random_integer = uni(gRandomNumberGenerator);
return random_integer;
}
UINT32 GetRndNum(UINT32 maxnum)
{
if (is_networked && is_client)
@@ -52,8 +73,6 @@ UINT32 GetRndNum(UINT32 maxnum)
{
GetCursorPos(&pt);// Get cursor location
srand(maxnum ^ rnd ^ pt.x ^ pt.y ^ GetTickCount());
//SendFmtMsg("Random Number Generator Reinitialized.");
//for(int l=0;l<100;l++)SendFmtMsg("%2d", Random(100));
}
if(maxnum == 0)
return(0);
+15 -6
View File
@@ -5,6 +5,7 @@
#include "Types.h"
#include "Debug.h"
#include "GameSettings.h"
//IMPORTANT: Changing this define will invalidate the JA2 save. If this is necessary, please ifdef your own value.
#define MAX_PREGENERATED_NUMS 256
@@ -18,15 +19,22 @@ extern void InitializeRandom(void);
extern UINT32 GetRndNum(UINT32 maxnum);
extern bool gfMPDebugOutputRandoms;
UINT32 NewRandom(UINT32 max);
extern GAME_EXTERNAL_OPTIONS gGameExternalOptions;
inline UINT32 Random(UINT32 uiRange)
{
return(GetRndNum(uiRange));
if (gGameExternalOptions.fNewRandom)
return NewRandom(uiRange);
else
return GetRndNum(uiRange);
}
inline INT32 iRandom(UINT32 uiRange)
/*inline INT32 iRandom(UINT32 uiRange)
{
return(GetRndNum(uiRange));
}
}*/
inline BOOLEAN Chance( UINT32 uiChance )
{
@@ -35,7 +43,8 @@ inline BOOLEAN Chance( UINT32 uiChance )
inline UINT32 PreRandom(UINT32 uiRange)
{
return(GetRndNum(uiRange));
return Random(uiRange);
//return(GetRndNum(uiRange));
}
inline BOOLEAN PreChance( UINT32 uiChance )
@@ -70,13 +79,13 @@ inline UINT32 Random(UINT32 uiRange)
// Returns a pseudo-random integer between 0 and uiRange-1
inline INT32 iRandom(UINT32 uiRange)
/*inline INT32 iRandom(UINT32 uiRange)
{
// Always return 0, if no range given (it's not an error)
if (uiRange == 0)
return(0);
return rand() * uiRange / RAND_MAX % uiRange;
}
}*/
//Chance( 74 ) returns TRUE 74% of the time. If uiChance >= 100, then it will always return TRUE.