X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Frng%2Fclass_RandomNumberGenerator.php;h=a30fb0ddd4750cf6e89d8a008fa92db30b1573fb;hp=f656432286755dcf0ea7788eac360348e4b140d5;hb=bde1a7331a8a5c1e4a304583c0db268026aeb8a8;hpb=cfdb4dd9f2ef3f8ef1db550fb81b7fc6e1eb7842 diff --git a/inc/classes/main/rng/class_RandomNumberGenerator.php b/inc/classes/main/rng/class_RandomNumberGenerator.php index f6564322..a30fb0dd 100644 --- a/inc/classes/main/rng/class_RandomNumberGenerator.php +++ b/inc/classes/main/rng/class_RandomNumberGenerator.php @@ -2,11 +2,11 @@ /** * A standard random number generator * - * @author Roland Haeder + * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org + * @link http://www.shipsimu.org * * 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 @@ -47,6 +47,11 @@ class RandomNumberGenerator extends BaseFrameworkSystem { */ private $rndStrLen = 0; + /** + * Self instance + */ + private static $selfInstance = NULL; + /** * Protected constructor * @@ -64,12 +69,21 @@ class RandomNumberGenerator extends BaseFrameworkSystem { * @param $extraInstance An extra instance for more salt (default: null) * @return $rngInstance An instance of this random number generator */ - public static final function createRandomNumberGenerator (FrameworkInterface $extraInstance = null) { - // Get a new instance - $rngInstance = new RandomNumberGenerator(); + public static final function createRandomNumberGenerator (FrameworkInterface $extraInstance = NULL) { + // Is self instance set? + if (is_null(self::$selfInstance)) { + // Get a new instance + $rngInstance = new RandomNumberGenerator(); - // Initialize the RNG now - $rngInstance->initRng($extraInstance); + // Initialize the RNG now + $rngInstance->initRng($extraInstance); + + // Set it "self" + self::$selfInstance = $rngInstance; + } else { + // Use from self instance + $rngInstance = self::$selfInstance; + } // Return the instance return $rngInstance; @@ -91,7 +105,7 @@ class RandomNumberGenerator extends BaseFrameworkSystem { $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'; @@ -108,11 +122,11 @@ class RandomNumberGenerator extends BaseFrameworkSystem { $this->fixedSalt = sha1( $serverIp . ':' . $extraInstance->__toString() . ':' . - serialize($this->getDatabaseInstance()->getConnectionData()) + json_encode($this->getDatabaseInstance()->getConnectionData()) ); } else { // Without extra information - $this->fixedSalt = sha1($serverIp . ':' . serialize($this->getDatabaseInstance()->getConnectionData())); + $this->fixedSalt = sha1($serverIp . ':' . json_encode($this->getDatabaseInstance()->getConnectionData())); } // One-way data we need for "extra-salting" the random number