note 99090 deleted from function.mt-rand by danbrown

From: Date: Fri, 30 Jul 2010 16:41:44 +0000
Subject: note 99090 deleted from function.mt-rand by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-171268@lists.php.net to get a copy of this message
Note Submitter: daniel at danielnorton dot com ---- <?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; }

« previous php.notes (#171268) next »