简体   繁体   English

PHP:如何创建唯一的有序列表?

[英]PHP: How to create a unique ordered list?

This is for a game. 这是一个游戏。 I have an array with 52 elements in it. 我有一个包含52个元素的数组。 I want to be able to create a unique list of these elements each time, but don't want to use randomized because it runs the risk of having the first few and last elements be the same at the start or end of the sequence. 我希望每次都能创建这些元素的唯一列表,但不想使用随机化,因为这样做会冒着在序列的开头或结尾使前几个元素和最后一个元素相同的风险。

I'm thinking how can I approach this if not using a truly randomized list. 我在想,如果不使用真正的随机列表,该如何处理。 So I thought, what about if I start off to seed the list with a randomized list, and then each time after that it does a unique method of ordering the list, so while it appears to be random it doesn't duplicate the first few and last elements. 所以我想,如果我开始使用随机列表为列表添加种子,然后每次之后,它都会执行一种独特的排序列表的方法,因此,尽管它看起来是随机的,但不会重复前几个和最后一个元素。

So one method I'm considering is to start in the middle of the randomized list and go up one element and down one element to create the new order of the list. 因此,我正在考虑的一种方法是从随机列表的中间开始,然后向上移动一个元素并向下移动一个元素以创建列表的新顺序。 Then each time after that, do the same sort of shuffling for lack of a better term. 然后在此之后每次都进行相同的改组,因为缺少更好的术语。

Are there other approaches to doing this that might be popular but I'm not considering? 还有其他可能流行的方法,但我没有考虑吗?

I started to write this in PHP, but my code isn't working. 我开始用PHP编写此代码,但是我的代码无法正常工作。 Not sure what I'm doing wrong, so you can kindly comment on it if you feel this is a good approach for the ordering even though my PHP code isn't working. 不知道我在做什么错,因此,即使我的PHP代码无法正常工作,如果您认为这是订购的一种好方法,也可以对此发表评论。 Thanks! 谢谢!

// First create an array of 52 elements, numbered 1…52.
for ($number=1; $number<=52; $number++)
    {
    $sequential_list[$number] = $number;
    }

print_r($sequential_list);

// Find the middle of $sequential_list

$middle = 26;

// Set the $middle as the first element in the new_list to seed it.
// Now go up one and down one from the $middle
    $new_list[1] = $sequential_list[$middle];
    $up = $middle;
    $down = $middle;
$index = 2; 

while ($index<=52)
    {
    $new_list[$index] = $sequential_list[$up++];
    echo "up is: " . $up . "\n";
    $new_list[$index++] = $sequential_list[$down--];
    }

print_r($new_list);

A simple example would be an array of 52 elements which are numbered 1 to 52. It would start in the middle, go up one and down one. 一个简单的示例是一个由52个元素组成的数组,它们的编号从1到52。它从中间开始,上移一个,下移一个。 So that 1 = 26, 2 = 27, 3 = 25, 4 = 28, 5 = 24, 6 = 29, 7 = 23, etc. 因此1 = 26、2 = 27、3 = 25、4 = 28、5 = 24、6 = 29、7 = 23等

$middle = rand(5,47);//as not to get the first and last five elements int the same order

$first = array_slice($sequential_list, 0, $middle);
$second = array_slice($sequential_list, $middle);

shuffle($first);
shuffle($second);

$random_list = array_merge($second, $first);

For the next iteration you start again from the new random_list. 对于下一次迭代,您将从新的random_list重新开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM