add new methods to PseudoRandom & move this class to util::

This commit is contained in:
MihailRis
2024-09-24 22:37:31 +03:00
parent c987bbd34f
commit 36eed38b4c
3 changed files with 62 additions and 49 deletions
+54 -40
View File
@@ -4,49 +4,63 @@
#include <ctime>
class PseudoRandom {
unsigned short seed;
public:
PseudoRandom() {
seed = (unsigned short)time(0);
}
namespace util {
class PseudoRandom {
unsigned short seed;
public:
PseudoRandom() {
seed = static_cast<unsigned short>(time(0));
}
int rand() {
seed = (seed + 0x7ed5 + (seed << 6));
seed = (seed ^ 0xc23c ^ (seed >> 9));
seed = (seed + 0x1656 + (seed << 3));
seed = ((seed + 0xa264) ^ (seed << 4));
seed = (seed + 0xfd70 - (seed << 3));
seed = (seed ^ 0xba49 ^ (seed >> 8));
int rand() {
seed = (seed + 0x7ed5 + (seed << 6));
seed = (seed ^ 0xc23c ^ (seed >> 9));
seed = (seed + 0x1656 + (seed << 3));
seed = ((seed + 0xa264) ^ (seed << 4));
seed = (seed + 0xfd70 - (seed << 3));
seed = (seed ^ 0xba49 ^ (seed >> 8));
return (int)seed;
}
return static_cast<int>(seed);
}
int32_t rand32() {
return (rand() << 16) | rand();
}
int32_t rand32() {
return (rand() << 16) | rand();
}
uint32_t randU32() {
return (rand() << 16) | rand();
}
uint32_t randU32() {
return (rand() << 16) | rand();
}
int64_t rand64() {
uint64_t x = randU32();
uint64_t y = randU32();
return (x << 32ULL) | y;
}
int64_t rand64() {
uint64_t x = randU32();
uint64_t y = randU32();
return (x << 32ULL) | y;
}
void setSeed(int number) {
seed =
((unsigned short)(number * 23729) ^ (unsigned short)(number + 16786)
);
rand();
}
void setSeed(int number1, int number2) {
seed =
(((unsigned short)(number1 * 23729) |
(unsigned short)(number2 % 16786)) ^
(unsigned short)(number2 * number1));
rand();
}
};
uint64_t randU64() {
uint64_t x = randU32();
uint64_t y = randU32();
return (x << 32ULL) | y;
}
float randFloat() {
return randU32() / static_cast<float>(UINT32_MAX);
}
double randDouble() {
return randU64() / static_cast<double>(UINT64_MAX);
}
void setSeed(int number) {
seed = (static_cast<unsigned short>(number * 23729) ^
static_cast<unsigned short>(number + 16786));
rand();
}
void setSeed(int number1, int number2) {
seed = ((static_cast<unsigned short>(number1 * 23729) |
static_cast<unsigned short>(number2 % 16786)) ^
static_cast<unsigned short>(number2 * number1));
rand();
}
};
}