]> git.mxchange.org Git - core.git/blobdiff - inc/classes/main/rng/class_RandomNumberGenerator.php
Several debug things (e.g. for timing) added
[core.git] / inc / classes / main / rng / class_RandomNumberGenerator.php
index 857478fb61069df8fc1ac2fd0064940820f60a2d..0580b87db376c9b974aac8e367d50addadd72908 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -56,10 +56,6 @@ class RandomNumberGenerator extends BaseFrameworkSystem {
        protected function __construct ($className = __CLASS__) {
                // Call parent constructor
                parent::__construct($className);
-
-               // Clean up a little
-               $this->removeNumberFormaters();
-               $this->removeSystemArray();
        }
 
        /**
@@ -68,7 +64,7 @@ class RandomNumberGenerator extends BaseFrameworkSystem {
         * @param       $extraInstance  An extra instance for more salt (default: null)
         * @return      $rngInstance    An instance of this random number generator
         */
-       public final static function createRandomNumberGenerator (FrameworkInterface $extraInstance = null) {
+       public static final function createRandomNumberGenerator (FrameworkInterface $extraInstance = NULL) {
                // Get a new instance
                $rngInstance = new RandomNumberGenerator();
 
@@ -92,10 +88,10 @@ class RandomNumberGenerator extends BaseFrameworkSystem {
 
                // Calculate the extra number which is always the same unless you give
                // a better prime number
-               $this->extraNumber = ($this->prime * $this->prime / (pi() ^ 2));
+               $this->extraNumber = ($this->prime * $this->prime / pow(pi(), 2));
 
                // Seed mt_rand()
-               mt_srand((double) sqrt(microtime() * 100000000 * $this->extraNumber));
+               mt_srand((double) sqrt(microtime(true) * 100000000 * $this->extraNumber));
 
                // Set the server IP to cluster
                $serverIp = 'cluster';
@@ -109,14 +105,23 @@ class RandomNumberGenerator extends BaseFrameworkSystem {
                // Yet-another fixed salt. This is not dependend on server software or date
                if ($extraInstance instanceof FrameworkInterface) {
                        // With extra instance information
-                       $this->fixedSalt = sha1($serverIp . ':' . $extraInstance->__toString() . ':' . serialize($this->getDatabaseInstance()->getConnectionData()));
+                       $this->fixedSalt = sha1(
+                               $serverIp . ':' .
+                               $extraInstance->__toString() . ':' .
+                               serialize($this->getDatabaseInstance()->getConnectionData())
+                       );
                } else {
                        // Without extra information
                        $this->fixedSalt = sha1($serverIp . ':' . serialize($this->getDatabaseInstance()->getConnectionData()));
                }
 
                // One-way data we need for "extra-salting" the random number
-               $this->extraSalt = sha1($this->fixedSalt . ':' . getenv('SERVER_SOFTWARE') . ':' . $this->getConfigInstance()->getConfigEntry('date_key') . $this->getConfigInstance()->getConfigEntry('base_url'));
+               $this->extraSalt = sha1(
+                       $this->fixedSalt . ':' .
+                       getenv('SERVER_SOFTWARE') . ':' .
+                       $this->getConfigInstance()->getConfigEntry('date_key') . ':' .
+                       $this->getConfigInstance()->getConfigEntry('base_url')
+               );
 
                // Get config entry for max salt length
                $this->rndStrLen = $this->getConfigInstance()->getConfigEntry('rnd_str_length');
@@ -130,7 +135,9 @@ class RandomNumberGenerator extends BaseFrameworkSystem {
         */
        public function randomString ($length = -1) {
                // Is the number <1, then fix it to default length
-               if ($length < 1) $length = $this->rndStrLen;
+               if ($length < 1) {
+                       $length = $this->rndStrLen;
+               } // END - if
 
                // Initialize the string
                $randomString = '';
@@ -139,7 +146,7 @@ class RandomNumberGenerator extends BaseFrameworkSystem {
                for ($idx = 0; $idx < $length; $idx++) {
                        // Add a random character and add it to our string
                        $randomString .= chr($this->randomNumber(0, 255));
-               }
+               } // END - for
 
                // Return the random string a little mixed up
                return str_shuffle($randomString);
@@ -178,7 +185,7 @@ class RandomNumberGenerator extends BaseFrameworkSystem {
        /**
         * Generates a key based on if we have extra (default) or fixed salt enabled
         *
-        * @return      $key    The generated key for encrypion
+        * @return      $key    The generated key for encryption
         */
        public function generateKey () {
                // Default is extra salt