note 99090 added to function.mt-rand
| From: | daniel at danielnorton dot com | Date: | Tue, 27 Jul 2010 06:41:56 +0000 |
| Subject: | note 99090 added to function.mt-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-171160@lists.php.net to get a copy of this message | ||
<?php
if (!defined('PHP_INT_BITS')) {
$GLOBALS['PHP_INT_BITS'] = PHP_INT_SIZE << 3;
define('PHP_INT_BITS',$GLOBALS['PHP_INT_BITS']);
}
if (!defined('PHP_MT_RAND_BITS') && function_exists('mt_getrandmax')) {
$GLOBALS['PHP_MT_RAND_BITS'] = (strlen(dechex(mt_getrandmax()))<<2)-1;
define('PHP_MT_RAND_BITS',$GLOBALS['PHP_MT_RAND_BITS']);
}
/**
* fetch_random_bits()
*
* Return the specified number of random bits in the lower bits of
* an integer.
*
* We actually change the order of the bits from our source, but who
* cares? They're random! (If the source is pseudo-random and the
* same input sequence is given, we'll provide the same output
* sequence.)
*
* More important for a pseudo-random source: we don't waste any
* bits.
*
*/
function fetch_random_bits($need = 32) {
static $buffer;
static $buffer_size = 0;
// don't fetch more bits than we can fit in an integer
$need = min($need,PHP_INT_BITS);
$bits = 0;
while ($need > 0) {
if ($buffer_size <= 0) {
// replenish our buffer
$buffer = mt_rand();
$buffer_size = PHP_MT_RAND_BITS;
}
// transfer the bits over
$partial = min($buffer_size,$need);
$bits = ($bits << $partial) | ($buffer & ~(PHP_INT_MAX << $partial));
$buffer >>= $partial;
$need -= $partial;
$buffer_size -= $partial;
}
return $bits;
}
----
Server IP: 69.147.83.197
Probable Submitter: 71.42.141.44
----
Manual Page -- http://www.php.net/manual/en/function.mt-rand.php
Edit -- https://master.php.net/note/edit/99090
Del: integrated -- https://master.php.net/note/delete/99090/integrated
Del: useless -- https://master.php.net/note/delete/99090/useless
Del: bad code -- https://master.php.net/note/delete/99090/bad+code
Del: spam -- https://master.php.net/note/delete/99090/spam
Del: non-english -- https://master.php.net/note/delete/99090/non-english
Del: in docs -- https://master.php.net/note/delete/99090/in+docs
Del: other reasons-- https://master.php.net/note/delete/99090
Reject -- https://master.php.net/note/reject/99090
Search -- https://master.php.net/manage/user-notes.php