2010-03-18 15:07:11 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
2021-12-26 18:47:58 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2010-03-18 15:07:11 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-12-26 18:47:58 +01:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-02-18 02:34:18 +01:00
|
|
|
*
|
2010-03-18 15:07:11 +00:00
|
|
|
*/
|
|
|
|
|
2021-08-15 13:14:17 +03:00
|
|
|
#include <limits.h>
|
2021-08-02 00:32:36 +03:00
|
|
|
|
2010-03-18 15:07:11 +00:00
|
|
|
#include "common/random.h"
|
|
|
|
#include "common/system.h"
|
2013-05-17 00:18:09 +03:00
|
|
|
#include "gui/EventRecorder.h"
|
2010-03-18 15:07:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
2011-05-16 16:35:10 +02:00
|
|
|
RandomSource::RandomSource(const String &name) {
|
|
|
|
// Use system time as RNG seed. Normally not a good idea, if you are using
|
|
|
|
// a RNG for security purposes, but good enough for our purposes.
|
|
|
|
assert(g_system);
|
2013-07-06 23:54:45 -04:00
|
|
|
|
|
|
|
#ifdef ENABLE_EVENTRECORDER
|
|
|
|
setSeed(g_eventRec.getRandomSeed(name));
|
|
|
|
#else
|
2021-08-28 15:27:26 -05:00
|
|
|
TimeDate time;
|
|
|
|
g_system->getTimeAndDate(time);
|
|
|
|
uint32 newSeed = time.tm_sec + time.tm_min * 60 + time.tm_hour * 3600;
|
|
|
|
newSeed += time.tm_mday * 86400 + time.tm_mon * 86400 * 31;
|
|
|
|
newSeed += time.tm_year * 86400 * 366;
|
|
|
|
newSeed = newSeed * 1000 + g_system->getMillis();
|
|
|
|
setSeed(newSeed);
|
2013-07-06 23:54:45 -04:00
|
|
|
#endif
|
2011-05-16 16:35:10 +02:00
|
|
|
}
|
|
|
|
|
2010-03-18 15:07:11 +00:00
|
|
|
void RandomSource::setSeed(uint32 seed) {
|
2021-09-09 14:46:08 -04:00
|
|
|
if (seed == 0)
|
|
|
|
seed++;
|
2010-03-18 15:07:11 +00:00
|
|
|
_randSeed = seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint RandomSource::getRandomNumber(uint max) {
|
2021-09-09 14:46:08 -04:00
|
|
|
scrambleSeed();
|
2021-08-02 00:32:36 +03:00
|
|
|
if (max == UINT_MAX)
|
2021-09-09 14:46:08 -04:00
|
|
|
return (_randSeed * 0xDEADBF03);
|
|
|
|
return (_randSeed * 0xDEADBF03) % (max + 1);
|
2010-03-18 15:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint RandomSource::getRandomBit() {
|
2021-09-09 14:46:08 -04:00
|
|
|
scrambleSeed();
|
2010-03-18 15:07:11 +00:00
|
|
|
return _randSeed & 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint RandomSource::getRandomNumberRng(uint min, uint max) {
|
|
|
|
return getRandomNumber(max - min) + min;
|
|
|
|
}
|
|
|
|
|
2020-10-25 00:40:59 +02:00
|
|
|
int RandomSource::getRandomNumberRngSigned(int min, int max) {
|
|
|
|
return getRandomNumber(max - min) + min;
|
|
|
|
}
|
2021-09-09 14:46:08 -04:00
|
|
|
|
|
|
|
inline void RandomSource::scrambleSeed() {
|
|
|
|
//marsaglia's paper says that any of 81 triplets are feasible
|
|
|
|
//(11,21,13) was chosen, with (cba) and (>>,<<,>>)
|
|
|
|
_randSeed ^= _randSeed >> 13;
|
|
|
|
_randSeed ^= _randSeed << 21;
|
|
|
|
_randSeed ^= _randSeed >> 11;
|
|
|
|
}
|
2020-10-25 00:40:59 +02:00
|
|
|
|
2013-01-26 19:33:27 +01:00
|
|
|
} // End of namespace Common
|