Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013 #include "random_func.hpp"
00014 #include "bitmath_func.hpp"
00015
00016 Randomizer _random, _interactive_random;
00017
00022 uint32 Randomizer::Next()
00023 {
00024 const uint32 s = this->state[0];
00025 const uint32 t = this->state[1];
00026
00027 this->state[0] = s + ROR(t ^ 0x1234567F, 7) + 1;
00028 return this->state[1] = ROR(s, 3) - 1;
00029 }
00030
00036 uint32 Randomizer::Next(uint32 max)
00037 {
00038 return ((uint64)this->Next() * (uint64)max) >> 32;
00039 }
00040
00045 void Randomizer::SetSeed(uint32 seed)
00046 {
00047 this->state[0] = seed;
00048 this->state[1] = seed;
00049 }
00050
00055 void SetRandomSeed(uint32 seed)
00056 {
00057 _random.SetSeed(seed);
00058 _interactive_random.SetSeed(seed * 0x1234567);
00059 }
00060
00061 #ifdef RANDOM_DEBUG
00062 #include "../network/network.h"
00063 #include "../network/network_server.h"
00064 #include "../network/network_internal.h"
00065 #include "../company_func.h"
00066 #include "../fileio_func.h"
00067 #include "../date_func.h"
00068
00069 uint32 DoRandom(int line, const char *file)
00070 {
00071 if (_networking && (!_network_server || (NetworkClientSocket::IsValidID(0) && NetworkClientSocket::Get(0)->status != NetworkClientSocket::STATUS_INACTIVE))) {
00072 static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
00073 if (f != NULL) {
00074 fprintf(f, "%08x; %02x; %04x; %02x; %s:%d\n", _date, _date_fract, _frame_counter, (byte)_current_company, file, line);
00075 fflush(f);
00076 }
00077 }
00078
00079 return _random.Next();
00080 }
00081
00082 uint32 DoRandomRange(uint32 max, int line, const char *file)
00083 {
00084 return ((uint64)DoRandom(line, file) * (uint64)max) >> 32;
00085 }
00086 #endif