diff options
author | Max Horn | 2010-03-18 15:07:11 +0000 |
---|---|---|
committer | Max Horn | 2010-03-18 15:07:11 +0000 |
commit | d78dba3bcae77e85107836cfeecaa958b7ebe4d4 (patch) | |
tree | ffe9793ec249ebe8bb429ce1cbeb9ef5155b0578 /common | |
parent | ef93d6921ee4dfed349093bd7378d99107d9e211 (diff) | |
download | scummvm-rg350-d78dba3bcae77e85107836cfeecaa958b7ebe4d4.tar.gz scummvm-rg350-d78dba3bcae77e85107836cfeecaa958b7ebe4d4.tar.bz2 scummvm-rg350-d78dba3bcae77e85107836cfeecaa958b7ebe4d4.zip |
COMMON: Move Common::RandomSource to common/random.*
svn-id: r48279
Diffstat (limited to 'common')
-rw-r--r-- | common/EventRecorder.cpp | 1 | ||||
-rw-r--r-- | common/EventRecorder.h | 2 | ||||
-rw-r--r-- | common/module.mk | 1 | ||||
-rw-r--r-- | common/random.cpp | 59 | ||||
-rw-r--r-- | common/random.h | 71 | ||||
-rw-r--r-- | common/util.cpp | 32 | ||||
-rw-r--r-- | common/util.h | 39 |
7 files changed, 134 insertions, 71 deletions
diff --git a/common/EventRecorder.cpp b/common/EventRecorder.cpp index 92cef51cf4..8d9856e45a 100644 --- a/common/EventRecorder.cpp +++ b/common/EventRecorder.cpp @@ -26,6 +26,7 @@ #include "common/EventRecorder.h" #include "common/config-manager.h" +#include "common/random.h" DECLARE_SINGLETON(Common::EventRecorder) diff --git a/common/EventRecorder.h b/common/EventRecorder.h index af96790094..257d6f5ca0 100644 --- a/common/EventRecorder.h +++ b/common/EventRecorder.h @@ -37,6 +37,8 @@ namespace Common { +class RandomSource; + /** * Our generic event recorder. * diff --git a/common/module.mk b/common/module.mk index 23d7b326f0..3516e3cd4e 100644 --- a/common/module.mk +++ b/common/module.mk @@ -15,6 +15,7 @@ MODULE_OBJS := \ memorypool.o \ md5.o \ mutex.o \ + random.o \ str.o \ stream.o \ util.o \ diff --git a/common/random.cpp b/common/random.cpp new file mode 100644 index 0000000000..9fd9c33e30 --- /dev/null +++ b/common/random.cpp @@ -0,0 +1,59 @@ +/* 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. + * + * 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 2 + * of the License, or (at your option) any later version. + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + */ + +#include "common/random.h" +#include "common/system.h" + + +namespace Common { + +RandomSource::RandomSource() { + // 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); + uint32 seed = g_system->getMillis(); + setSeed(seed); +} + +void RandomSource::setSeed(uint32 seed) { + _randSeed = seed; +} + +uint RandomSource::getRandomNumber(uint max) { + _randSeed = 0xDEADBF03 * (_randSeed + 1); + _randSeed = (_randSeed >> 13) | (_randSeed << 19); + return _randSeed % (max + 1); +} + +uint RandomSource::getRandomBit() { + _randSeed = 0xDEADBF03 * (_randSeed + 1); + _randSeed = (_randSeed >> 13) | (_randSeed << 19); + return _randSeed & 1; +} + +uint RandomSource::getRandomNumberRng(uint min, uint max) { + return getRandomNumber(max - min) + min; +} + +} // End of namespace Common diff --git a/common/random.h b/common/random.h new file mode 100644 index 0000000000..8fc5c4cf7d --- /dev/null +++ b/common/random.h @@ -0,0 +1,71 @@ +/* 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. + * + * 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 2 + * of the License, or (at your option) any later version. + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + */ + +#ifndef COMMON_RANDOM_H +#define COMMON_RANDOM_H + +#include "common/scummsys.h" + +namespace Common { + +/** + * Simple random number generator. Although it is definitely not suitable for + * cryptographic purposes, it serves our purposes just fine. + */ +class RandomSource { +private: + uint32 _randSeed; + +public: + RandomSource(); + void setSeed(uint32 seed); + + uint32 getSeed() { + return _randSeed; + } + + /** + * Generates a random unsigned integer in the interval [0, max]. + * @param max the upper bound + * @return a random number in the interval [0, max] + */ + uint getRandomNumber(uint max); + /** + * Generates a random bit, i.e. either 0 or 1. + * Identical to getRandomNumber(1), but faster, hopefully. + * @return a random bit, either 0 or 1 + */ + uint getRandomBit(); + /** + * Generates a random unsigned integer in the interval [min, max]. + * @param min the lower bound + * @param max the upper bound + * @return a random number in the interval [min, max] + */ + uint getRandomNumberRng(uint min, uint max); +}; + +} // End of namespace Common + +#endif diff --git a/common/util.cpp b/common/util.cpp index e761e3fa01..e645ce639b 100644 --- a/common/util.cpp +++ b/common/util.cpp @@ -148,38 +148,6 @@ String tag2string(uint32 tag) { #pragma mark - -RandomSource::RandomSource() { - // 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); - uint32 seed = g_system->getMillis(); - setSeed(seed); -} - -void RandomSource::setSeed(uint32 seed) { - _randSeed = seed; -} - -uint RandomSource::getRandomNumber(uint max) { - _randSeed = 0xDEADBF03 * (_randSeed + 1); - _randSeed = (_randSeed >> 13) | (_randSeed << 19); - return _randSeed % (max + 1); -} - -uint RandomSource::getRandomBit() { - _randSeed = 0xDEADBF03 * (_randSeed + 1); - _randSeed = (_randSeed >> 13) | (_randSeed << 19); - return _randSeed & 1; -} - -uint RandomSource::getRandomNumberRng(uint min, uint max) { - return getRandomNumber(max - min) + min; -} - - -#pragma mark - - - const LanguageDescription g_languages[] = { {"zh-cn", "Chinese (China)", ZH_CNA}, {"zh", "Chinese (Taiwan)", ZH_TWN}, diff --git a/common/util.h b/common/util.h index 453eccda6f..3e1fc2d1c1 100644 --- a/common/util.h +++ b/common/util.h @@ -111,45 +111,6 @@ String tag2string(uint32 tag); #define tag2str(x) Common::tag2string(x).c_str() - - -/** - * Simple random number generator. Although it is definitely not suitable for - * cryptographic purposes, it serves our purposes just fine. - */ -class RandomSource { -private: - uint32 _randSeed; - -public: - RandomSource(); - void setSeed(uint32 seed); - - uint32 getSeed() { - return _randSeed; - } - - /** - * Generates a random unsigned integer in the interval [0, max]. - * @param max the upper bound - * @return a random number in the interval [0, max] - */ - uint getRandomNumber(uint max); - /** - * Generates a random bit, i.e. either 0 or 1. - * Identical to getRandomNumber(1), but faster, hopefully. - * @return a random bit, either 0 or 1 - */ - uint getRandomBit(); - /** - * Generates a random unsigned integer in the interval [min, max]. - * @param min the lower bound - * @param max the upper bound - * @return a random number in the interval [min, max] - */ - uint getRandomNumberRng(uint min, uint max); -}; - /** * List of game language. */ |