我需要根据种子号对数组进行洗牌,这样我就可以在需要时进行相同的洗牌。
例如:1. print_r( shuffleIt( $array, 2 ) );
2. print_r( shuffleIt( $array, 6 ) );
3. print_r( shuffleIt( $array, 2 ) );
- 3。将显示相同的洗牌数组,但不同于2。
我在google上找到了这个函数:
function entropy( $array, $sort_seed ) {
mt_srand( $sort_seed );
$order = array_map( create_function( '$val', 'return mt_rand( );' ), range( 1, count( $array ) ) );
array_multisort( $order, $array );
return $array;
}
它工作得很好在我的pc与php-cli,我总是得到相同的数组为每个不同的sort_seed我使用,但当我把它上传到服务器,我得到不同的数组每次即使我使用相同的sort_seed。
我怎么能得到总是相同的洗牌数组时使用相同的sort_seed?
顺便说一句。我需要保存键或对多维数组进行排序,以便我可以在那里存储键
如果你需要它在任何pc上,你需要一个随机数字生成器,在计算机上工作相同。我从维基百科的随机数生成页面中查找了一个可能适合您的伪随机数生成器,并将其放入一个类中,以便您可以播种它。
我不知道你需要它做什么,但它可能正适合你的需要。它独立于系统配置:
function shuffleIt($array, $seed)
{
$mwc = new mwc($seed);
$order = array();
$count = count($array);
while($count--)
$order[] = $mwc->random()
;
array_multisort($order, $array);
return $array;
}
/**
* Multiply-with-carry RNG
*
* method invented by George Marsaglia
*/
class mwc
{
private static $def_m_w = 1712; /* must not be zero */
private static $def_m_z = 23; /* must not be zero */
private $m_w, $m_z;
public function __construct($seed = NULL)
{
$this->m_w = self::$def_m_w;
$this->m_z = self::$def_m_z;
if (NULL !== $seed)
$this->seed($seed);
}
public function seed($seed)
{
$seed = (int) $seed;
if (!$seed) throw new InvalidArgumentException('Must not be zero.');
$this->m_z = $seed;
$this->random();
}
public function random()
{
$this->m_z = 36969 * ($this->m_z & 65535) + ($this->m_z >> 16);
$this->m_w = 18000 * ($this->m_w & 65535) + ($this->m_w >> 16);
return ($this->m_z << 16) + $this->m_w; /* 32-bit result */
}
}
注意:在32/64位系统之间可能表现不同,特别是PHP在windows和unix之间对整数和溢出的处理不同。您可能希望在PHP中为32位整数的有符号最小偏移量,而不是像现在这样为0,以切换到gmp实现或只是将大小减少一位。
使用示例来自荷兰的ekke报告32位工作
$shuffle = new GeorgeShuffle();
$seed = $shuffle->seed();
$a = array('A', 'B', 'C', 'D', 'E', 'F', 'G');
$shuffle->reOrder($a);
var_dump($a);
$shuffle->seed($seed);
$shuffle->reOrder($a);
var_dump($a);
/**
* Array shuffle class using
* the multiply-with-carry method
* invented by George Marsaglia
*/
class GeorgeShuffle
{
private static $def_m_w = 1959; /* must not be zero */
private static $def_m_z = 2006; /* must not be zero */
private $m_w, $m_z;
const maxint = 2147483647;
public function __construct($seed = null)
{
$this->m_w = self::$def_m_w;
$this->m_z = self::$def_m_z;
if ($seed) $this->seed($seed);
}
public function reOrder(&$array, $seed = null)
{
if (!empty($seed)) $this->seed($seed);
$a = array();
for ($i = 0, $j = count($array); $i < $j; $i++) {
$a[$i] = $this->random();
}
array_multisort($a, $array);
//- to return a copy, remove the &
return $array;
}
public function seed($seed = false)
{
if (is_string($seed)) $seed = hexdec($seed);
if (empty($seed)) $seed = round(mt_rand(1, self::maxint));
$this->m_z = $seed;
$this->random();
//- return the seed used in hex (8 chars) for reproducing
return str_pad(dechex($seed), 8, '0', STR_PAD_LEFT);
}
public function random()
{
$this->m_z = 36969 * (($this->m_z And 65535) + ($this->m_z >> 16));
$this->m_w = 18000 * (($this->m_w And 65535) + ($this->m_w >> 16));
return ($this->m_z << 16) + $this->m_w; /* 32-bit signed result */
}
}
您的主机是否偶然运行Suhosin安全扩展?显然,它有两个防止脚本设置种子的指令:
suhosin.srand.ignore = On
suhosin.mt_srand.ignore = On
进一步阅读:
- http://www.suspekt.org/2008/08/22/suhosin - 0926 -改进- randomness/
- http://www.adayinthelifeof.nl/2010/12/13/php-srand-problems-with-suhosin/
这里有一个快速的方法来测试是否存在这个问题:
<?php
mt_srand(33);
var_dump(mt_rand(1, 10000));
mt_srand(33);
var_dump(mt_rand(1, 10000));
mt_srand(33);
var_dump(mt_rand(1, 10000));