3 * A standard random number generator
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class RandomNumberGenerator extends BaseFrameworkSystem {
26 * Prime number for better pseudo random numbers
31 * Add this calculated number to the rng
33 private $extraNumber = 0;
36 * Extra salt for secured hashing
38 private $extraSalt = "";
41 * Fixed salt for secured hashing
43 private $fixedSalt = "";
46 * Maximum length for random string
48 private $rndStrLen = 0;
51 * Protected constructor
53 * @param $className Name of this class
56 protected function __construct ($className = __CLASS__) {
57 // Call parent constructor
58 parent::__construct($className);
60 // Set part description
61 $this->setObjectDescription("Standard random number generator");
63 // Create unique ID number
64 $this->generateUniqueId();
67 $this->removeNumberFormaters();
68 $this->removeSystemArray();
72 * Creates an instance of this class
74 * @return $rngInstance An instance of this random number generator
76 public final static function createRandomNumberGenerator () {
78 $rngInstance = new RandomNumberGenerator();
80 // Initialize the RNG now
81 $rngInstance->initRng();
83 // Return the instance
88 * Initializes the random number generator
92 protected function initRng () {
94 mt_srand((double) microtime() * 1000000);
96 // Get the prime number from config
97 $this->prime = $this->getConfigInstance()->readConfig('math_prime');
99 // Calculate the extra number which is always the same unless you give
100 // a better prime number
101 $this->extraNumber = ($this->prime * $this->prime / (pi() ^ 2));
103 // Set the server IP to cluster
104 $serverIp = "cluster";
106 // Do we have a single server?
107 if ($this->getConfigInstance()->readConfig('is_single_server') == "Y") {
108 // Then use that IP for extra security
109 $serverIp = getenv('SERVER_ADDR');
112 // Yet-another fixed salt. This is not dependend on server software or date
113 $this->fixedSalt = sha1($serverIp . ":" . serialize($this->getDatabaseInstance()->getConnectionData()));
115 // One-way data we need for "extra-salting" the random number
116 /* @TODO Add site key for stronger salt! */
117 $this->extraSalt = sha1($this->fixedSalt . ":" . getenv('SERVER_SOFTWARE') . ":" . $this->getConfigInstance()->readConfig('date_key'));
119 // Get config entry for max salt length
120 $this->rndStrLen = $this->getConfigInstance()->readConfig('rnd_str_length');
124 * Makes a pseudo-random string useable for salts
126 * @param $length Length of the string, default: 128
127 * @return $randomString The pseudo-random string
129 public function makeRandomString ($length = -1) {
130 // Is the number <1, then fix it to default length
131 if ($length < 1) $length = $this->rndStrLen;
133 // Initialize the string
137 for ($idx = 0; $idx < $length; $idx++) {
138 // Add a random character and add it to our string
139 $randomString .= chr($this->randomNumnber(0, 255));
142 // Return the random string a little mixed up
143 return str_shuffle($randomString);
147 * Generate a pseudo-random integer number in a given range
149 * @param $min Min value to generate
150 * @param $max Max value to generate
151 * @return $num Pseudo-random number
153 public function randomNumnber ($min, $max) {
154 /* @TODO I had a better random number generator here but now it is somewhere lost :( */
155 return mt_rand($min, $max);
159 * Getter for extra salt
163 public final function getExtraSalt () {
164 return $this->extraSalt;
168 * Getter for fixed salt
172 public final function getFixedSalt () {
173 return $this->fixedSalt;