class_RandomNumberGenerator.php

Go to the documentation of this file.
00001 <?php
00024 class RandomNumberGenerator extends BaseFrameworkSystem {
00028         private $prime = 0;
00029 
00033         private $extraNumber = 0;
00034 
00038         private $extraSalt = "";
00039 
00043         private $fixedSalt = "";
00044 
00048         private $rndStrLen = 0;
00049 
00056         protected function __construct ($className = __CLASS__) {
00057                 // Call parent constructor
00058                 parent::__construct($className);
00059 
00060                 // Clean up a little
00061                 $this->removeNumberFormaters();
00062                 $this->removeSystemArray();
00063         }
00064 
00071         public final static function createRandomNumberGenerator (FrameworkInterface $extraInstance = null) {
00072                 // Get a new instance
00073                 $rngInstance = new RandomNumberGenerator();
00074 
00075                 // Initialize the RNG now
00076                 $rngInstance->initRng($extraInstance);
00077 
00078                 // Return the instance
00079                 return $rngInstance;
00080         }
00081 
00089         protected function initRng ($extraInstance) {
00090                 // Get the prime number from config
00091                 $this->prime = $this->getConfigInstance()->readConfig('math_prime');
00092 
00093                 // Calculate the extra number which is always the same unless you give
00094                 // a better prime number
00095                 $this->extraNumber = ($this->prime * $this->prime / (pi() ^ 2));
00096 
00097                 // Seed mt_rand()
00098                 mt_srand((double) sqrt(microtime() * 100000000 * $this->extraNumber));
00099 
00100                 // Set the server IP to cluster
00101                 $serverIp = "cluster";
00102 
00103                 // Do we have a single server?
00104                 if ($this->getConfigInstance()->readConfig('is_single_server') === "Y") {
00105                         // Then use that IP for extra security
00106                         $serverIp = getenv('SERVER_ADDR');
00107                 } // END - if
00108 
00109                 // Yet-another fixed salt. This is not dependend on server software or date
00110                 if ($extraInstance instanceof FrameworkInterface) {
00111                         // With extra instance information
00112                         $this->fixedSalt = sha1($serverIp . ":" . $extraInstance->__toString() . ":" . serialize($this->getDatabaseInstance()->getConnectionData()));
00113                 } else {
00114                         // Without extra information
00115                         $this->fixedSalt = sha1($serverIp . ":" . serialize($this->getDatabaseInstance()->getConnectionData()));
00116                 }
00117 
00118                 // One-way data we need for "extra-salting" the random number
00119                 $this->extraSalt = sha1($this->fixedSalt . ":" . getenv('SERVER_SOFTWARE') . ":" . $this->getConfigInstance()->readConfig('date_key') . $this->getConfigInstance()->readConfig('base_url'));
00120 
00121                 // Get config entry for max salt length
00122                 $this->rndStrLen = $this->getConfigInstance()->readConfig('rnd_str_length');
00123         }
00124 
00131         public function randomString ($length = -1) {
00132                 // Is the number <1, then fix it to default length
00133                 if ($length < 1) $length = $this->rndStrLen;
00134 
00135                 // Initialize the string
00136                 $randomString = "";
00137 
00138                 // And generate it
00139                 for ($idx = 0; $idx < $length; $idx++) {
00140                         // Add a random character and add it to our string
00141                         $randomString .= chr($this->randomNumber(0, 255));
00142                 }
00143 
00144                 // Return the random string a little mixed up
00145                 return str_shuffle($randomString);
00146         }
00147 
00156         public function randomNumber ($min, $max) {
00157                 return mt_rand($min, $max);
00158         }
00159 
00165         public final function getExtraSalt () {
00166                 return $this->extraSalt;
00167         }
00168 
00174         public final function getFixedSalt () {
00175                 return $this->fixedSalt;
00176         }
00177 }
00178 
00179 // [EOF]
00180 ?>

Generated on Mon Dec 8 01:06:46 2008 for Ship-Simulator by  doxygen 1.5.6