洗牌的随机数在Javascript / PHP不重复 [英] Shuffles Random Numbers with no repetition in Javascript/PHP
问题描述
我已经通过这里的一些答案搜查,但似乎我需要或我只是不知道如何申请,虽然它的东西没有。
I've searched through some of the answers here but it doesn't seem the thing that I needed or I just don't know how to apply it though.
我还没有开始任何codeS和我只是在想如何做到这一点,我不知道该怎么做。我需要你的帮助球员。
I haven't started any codes and I'm only thinking on how to do it and I have no idea how to do it. I need your help guys.
让我们假设我有一个由这些值低于
Let's assume that I have an array which consists of these values below
[1,2,3,4,5,6,7,8,9]
和我需要它洗不重复的最后结果的每个数字的位置。所以它可能会像
And I need to shuffle it without repeating the position of each numbers of the last result. so it would probably like
[5,3,9,6,2,8,1,4,7]
如果我再次洗牌它,它会像
if I shuffle it again it would be like
[4,7,2,1,8,3,6,9,5]
等。
好吧,我不知道如果有任何相关性,但,宁可不使用兰特()虽然。这个东西任何解决方案?
Well I don't know if there' any relevance to it but, would rather not to use rand() though. Any solution for this stuff?
推荐答案
试试这个,
$count = 15;
$values = range(1, $count);
shuffle($values);
$values = array_slice($values, 0, 15);
或
$numbers = array();
do {
$possible = rand(1,15);
if (!isset($numbers[$possible])) {
$numbers[$possible] = true;
}
} while (count($numbers) < 15);
print_r(array_keys($numbers));
这可能会帮助你。
may this help you.
这篇关于洗牌的随机数在Javascript / PHP不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!