mirror of
https://github.com/glest/glest-source.git
synced 2025-08-20 15:11:20 +02:00
- attempt to use streflop for random number generation to fix AI synch issues on corss platform
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include "randomgen.h"
|
||||
#include <cassert>
|
||||
|
||||
#include "util.h"
|
||||
#include "leak_dumper.h"
|
||||
|
||||
namespace Shared { namespace Util {
|
||||
@@ -15,30 +15,61 @@ const int RandomGen::b= 150889;
|
||||
|
||||
RandomGen::RandomGen(){
|
||||
lastNumber= 0;
|
||||
|
||||
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] lastNumber = %d\n",__FILE__,__FUNCTION__,__LINE__,lastNumber);
|
||||
}
|
||||
|
||||
void RandomGen::init(int seed){
|
||||
|
||||
#ifdef STREFLOP_H
|
||||
lastNumber = math::RandomInit(seed); // streflop
|
||||
#else
|
||||
lastNumber= seed % m;
|
||||
#endif
|
||||
|
||||
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] seed = %d, lastNumber = %d\n",__FILE__,__FUNCTION__,__LINE__,seed,lastNumber);
|
||||
}
|
||||
|
||||
int RandomGen::rand(){
|
||||
int RandomGen::rand() {
|
||||
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] lastNumber = %d\n",__FILE__,__FUNCTION__,__LINE__,lastNumber);
|
||||
|
||||
lastNumber= (a*lastNumber + b) % m;
|
||||
|
||||
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] lastNumber = %d\n",__FILE__,__FUNCTION__,__LINE__,lastNumber);
|
||||
|
||||
return lastNumber;
|
||||
}
|
||||
|
||||
int RandomGen::randRange(int min, int max){
|
||||
assert(min<=max);
|
||||
|
||||
#ifdef STREFLOP_H
|
||||
int res = math::Random<true, false, float>(min, max); // streflop
|
||||
#else
|
||||
int diff= max-min;
|
||||
int res= min + static_cast<int>(static_cast<float>(diff+1)*RandomGen::rand() / m);
|
||||
#endif
|
||||
assert(res>=min && res<=max);
|
||||
|
||||
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] min = %d, max = %d, res = %d\n",__FILE__,__FUNCTION__,__LINE__,min,max,res);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
float RandomGen::randRange(float min, float max){
|
||||
assert(min<=max);
|
||||
|
||||
#ifdef STREFLOP_H
|
||||
float res = math::Random<true, false, float>(min, max); // streflop
|
||||
#else
|
||||
float rand01= static_cast<float>(RandomGen::rand())/(m-1);
|
||||
float res= min+(max-min)*rand01;
|
||||
#endif
|
||||
|
||||
assert(res>=min && res<=max);
|
||||
|
||||
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] min = %f, max = %f, res = %f\n",__FILE__,__FUNCTION__,__LINE__,min,max,res);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user