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 * Maximum length for random string
43 private $rndStrLen = 0;
46 * Protected constructor
48 * @param $className Name of this class
51 protected function __construct ($className = __CLASS__) {
52 // Call parent constructor
53 parent::__construct($className);
55 // Set part description
56 $this->setObjectDescription("Standard random number generator");
58 // Create unique ID number
59 $this->generateUniqueId();
62 $this->removeNumberFormaters();
63 $this->removeSystemArray();
67 * Creates an instance of this class
69 * @return $rngInstance An instance of this random number generator
71 public final static function createRandomNumberGenerator () {
73 $rngInstance = new RandomNumberGenerator();
75 // Initialize the RNG now
76 $rngInstance->initRng();
78 // Return the instance
83 * Initializes the random number generator
87 protected function initRng () {
89 mt_srand((double) microtime() * 1000000);
91 // Get the prime number from config
92 $this->prime = $this->getConfigInstance()->readConfig('math_prime');
94 // Calculate the extra number which is always the same unless you give
95 // a better prime number
96 $this->extraNumber = ($this->prime * $this->prime / (pi() ^ 2));
98 // Set the server IP to cluster
99 $serverIp = "cluster";
101 // Do we have a single server?
102 if ($this->getConfigInstance()->readConfig('is_single_server') == "Y") {
103 // Then use that IP for extra security
104 $serverIp = getenv('SERVER_ADDR');
107 // One-way data we need for "extra-salting" the random number
108 // @TODO Add site key for stronger salt!
109 $this->extraSalt = sha1($serverIp . ":" . getenv('SERVER_SOFTWARE') . ":" . $this->getConfigInstance()->readConfig('date_key') . ":" . serialize($this->getDatabaseInstance()->getConnectionData()));
111 // Get config entry for max salt length
112 $this->rndStrLen = $this->getConfigInstance()->readConfig('rnd_str_length');
116 * Makes a pseudo-random string useable for salts
118 * @param $length Length of the string, default: 128
119 * @return $randomString The pseudo-random string
121 public function makeRandomString ($length = -1) {
122 // Is the number <1, then fix it to default length
123 if ($length < 1) $length = $this->rndStrLen;
125 // Initialize the string
129 for ($idx = 0; $idx < $length; $idx++) {
130 // Add a random character and add it to our string
131 $randomString .= chr($this->randomNumnber(0, 255));
134 // Return the random string mixed up
135 return str_shuffle($randomString);
139 * Generate a pseudo-random integer number in a given range
141 * @param $min Min value to generate
142 * @param $max Max value to generate
143 * @return $num Pseudo-random number
145 public function randomNumnber ($min, $max) {
146 // @TODO I had a better random number generator here
147 return mt_rand($min, $max);
151 * Getter for extra salt
155 public final function getExtraSalt () {
156 return $this->extraSalt;