生成predictable-suffled乱阵 [英] Generate predictable-suffled random array

查看:177
本文介绍了生成predictable-suffled乱阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,

问题

这是众所周知了解伪随机数。 伪实际上意味着,尽管它们是随机的(即未predictable)在一般情况下,他们仍然会在序列,在相同的发电机初值使用相同的。例如,在PHP中有 mt_srand()函数做到这一点。例如:

It's well known about pseudo-random numbers. 'Pseudo' actually means, that, despite they are random (i.e. unpredictable) in general, they still will be same in sequence, in which same generator init value was used. For example, in PHP there's mt_srand() function to do that. Example:

mt_srand(1);
var_dump(mt_rand(), mt_rand(), mt_rand());

- 无事,多少时间,我们将推出我们的脚本:生成三个号码始终处于顺序相同。

-no matter, how many time we'll launch our script: generated three numbers will always be same in sequence.

现在,我的问题是如何做同样的 - 但对于洗牌数组。即我想创建一个函数,该函数将接受输入数组洗牌和的。在相同的种子值洗牌必须有连续的顺序相同。即让我们调用该函数 shuffleWithSeed() - 然后下面应该为每一个脚本启动:

Now, my issue is how to do the same - but for shuffling array. I.e. I want to create a function, which will accept input array to shuffle and seed. Within same seed value shuffling must have consecutive same order. I.e. let we call that function shuffleWithSeed() - and then following should work for every script launch:

$input = ['foo', 'bar', 'baz'];
$test  = shuffleWithSeed($input, 1000);//1000 is just some constant value
var_dump($test); //let it be ['bar', 'foo', 'baz']
$test  = shuffleWithSeed($test, 1000); 
var_dump($test); //let it be ['baz', 'foo', 'bar']
$test  = shuffleWithSeed($test, 1000); 
var_dump($test); //let it be ['baz', 'bar', 'foo']
//...

-i.e。无论有多少次,我们会尽洗牌对我们的阵列 - 我要为下一个脚本启动排序顺序将永远有一个值的范围内是相同的。

-i.e. no matter how many times we'll do shuffle for our array - I want for the next script launch ordering sequence will be always the same within one seed value.

我的做法

我心目中这种算法:

  1. 在初始化随机数发生器通过
  2. 生成 N 随机数,其中 N $输入成员
  3. 排序从第2步
  4. 号码
  5. 请相应的数字依赖从 $输入键。
  1. Initialize random numbers generator with passed seed
  2. Generate N random numbers, where N is the number of $input members
  3. Sort numbers from step 2
  4. Make corresponding numbers be dependent from $input keys.

我在实现这一点:

function shuffleWithSeed(array $input, $seed=null)
{
   if(!isset($seed))
   {
      shuffle($input);
      return $input;
   }
   if(!is_int($seed))
   {
      throw new InvalidArgumentException('Invalid seed value');
   }
   mt_srand($seed);
   $random = [];
   foreach($input as $key=>$value)
   {
      $random[$key] = mt_rand();
   }
   asort($random);
   $random = array_combine(array_keys($random), array_values($input));
   ksort($random);
   return $random;
}

- 现在,还发现费雪耶茨算法 - 但不是知道它可以与伪随机数(即种子)

-now, also found Fisher-Yates algorithm - but not sure if it can work with pseudorandom numbers (i.e. with seed)

问题

正如你所看到的,我做两类在我的功能 - 首先由值和第二通过钥匙。

As you can see, I'm doing two sorts in my function - first by values and second by keys.

  • 在这个问题能有一个排序呢?或者没有排序呢?输入阵列可能会很大,所以我想避免这种情况。
  • 但是,可能是我的算法是不是很好?如果是的话,还有什么其他的选择可以建议?

推荐答案

下面是一个副本的功能我已经实现了前一阵子就是出于这样的目的,并贴:

Here's a copy and paste of a function I have implemented a while ago for exactly this purpose:

/**
 * Shuffles an array in a repeatable manner, if the same $seed is provided.
 * 
 * @param array &$items The array to be shuffled.
 * @param integer $seed The result of the shuffle will be the same for the same input ($items and $seed). If not given, uses the current time as seed.
 * @return void
 */
protected function seeded_shuffle(array &$items, $seed = false) {
    $items = array_values($items);
    mt_srand($seed ? $seed : time());
    for ($i = count($items) - 1; $i > 0; $i--) {
        $j = mt_rand(0, $i);
        list($items[$i], $items[$j]) = array($items[$j], $items[$i]);
    }
}

它实现了一个简单的费雪耶茨洗牌一个种子随机数发生器。

It implements a simple Fisher-Yates shuffle with a seeded random number generator.

这篇关于生成predictable-suffled乱阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆