note 97977 modified in function.mt-rand by visualmind

From: Date: Wed, 19 May 2010 09:22:22 +0000
Subject: note 97977 modified in function.mt-rand by visualmind
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-169398@lists.php.net to get a copy of this message
A class to generate 99.5% unqiue strings. I found that there is only one or two characters common between two subsequent strings. <?php class Local_RandomString { protected $_length; protected $_prevRand; public function __construct($length = 15) { $this->_length = $length; } public function getRand() { $randStr = null; $args[] = 'N' . $this->_length; for($i = 0; $i < $this->_length; $i++) { $args[] = mt_rand(); } $randStr = substr(base64_encode((call_user_func_array('pack', $args))), 1, $this->_length); $this->_prevRand = $randStr; return $randStr; } public function setLength($l) { $this->_length = (int) $l; if($this->_length <= 0) { throw new Exception('Invalid random string length'); } } public function getPrevRand() { return $this->_prevRand; } } ?> --was-- A class to generate 99.5% unqiue strings. I found that there is only one or two characters common between two subsequent strings. <?php class Local_RandomString { protected $_length; protected $_prevRand; public function __construct($length = 15) { $this->_length = 10; } public function getRand() { $randStr = null; $args[] = 'N' . $this->_length; for($i = 0; $i < $this->_length; $i++) { $args[] = mt_rand(); } $randStr = substr(base64_encode((call_user_func_array('pack', $args))), 1, $this->_length); $this->_prevRand = $randStr; return $randStr; } public function setLength($l) { $this->_length = (int) $l; if($this->_length <= 0) { throw new Exception('Invalid random string length'); } } public function getPrevRand() { return $this->_prevRand; } } ?> http://php.net/manual/en/function.mt-rand.php

« previous php.notes (#169398) next »