1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include "random.h"
#include <cassert>
namespace Shared{ namespace Util{
// =====================================================
// class Random
// =====================================================
const int Random::m= 714025;
const int Random::a= 1366;
const int Random::b= 150889;
Random::Random(){
lastNumber= 0;
}
void Random::init(int seed){
lastNumber= seed % m;
}
int Random::rand(){
lastNumber= (a*lastNumber + b) % m;
return lastNumber;
}
int Random::randRange(int min, int max){
assert(min<=max);
int diff= max-min;
int res= min + static_cast<int>(static_cast<float>(diff+1)*Random::rand() / m);
assert(res>=min && res<=max);
return res;
}
float Random::randRange(float min, float max){
assert(min<=max);
float rand01= static_cast<float>(Random::rand())/(m-1);
float res= min+(max-min)*rand01;
assert(res>=min && res<=max);
return res;
}
}}//end namespace
|