简体   繁体   English

PHP-从键生成指定范围内的随机整数

[英]PHP - Generating random integers within specified range from a key

I have a set of questions with unique IDs in a MySQL database. 我在MySQL数据库中有一组具有唯一ID的问题。 Users also have a unique ID and are to answer these questions and their answers are saved in the database. 用户还具有唯一的ID,他们将回答这些问题,并将其答案保存在数据库中。

Now, I want users to get 5 non-repeating uniquely and randomly picked questions from the pool of available ones (let's say 50) based on users ID. 现在,我希望用户根据用户ID从可用的问题池(假设为50个)中获得5个唯一的非重复问题,并从中随机选择。 So when a user with id 10 starts answering his questions, but stops and wants to return later to the same page, he will get the same questions as before. 因此,当ID为10的用户开始回答他的问题,但停止并想稍后返回同一页面时,他将得到与以前相同的问题。 A user with id 11 will get a different random set of questions, but it will always be the same for him and different from all other users. ID为11的用户将获得不同的随机问题集,但对他来说总是相同的,并且与所有其他用户不同。

I found that random.org can generate exactly what I need with their sequence generator that generates a random sequence of numbers based on provided ID: https://www.random.org/sequences/?min=1&max=50&col=1&format=plain&rnd=id.10 But I would like the generation to be done locally instead of relying random.org API. 我发现random.org可以使用其序列生成器完全生成我需要的东西,该序列生成器根据提供的ID生成随机的数字序列: https ://www.random.org/sequences/?min=1&max=50&col=1&format=plain&rnd = id.10但我希望生成可以在本地完成,而不是依赖random.org API。

So, I need to generate 'X' unique random integers, within specified range 'Y' that are generated based on supplied integer 'Z'. 因此,我需要根据指定的整数“ Z”生成指定范围“ Y”内的“ X”个唯一随机整数。 I should be able to call a function with 'Z' as parameter and receive back the same 'X' integers every time. 我应该能够使用“ Z”作为参数来调用函数,并且每次都收到相同的“ X”整数。

I need to know how to replicate this generation with PHP code or at least a push or hint in a direction of a PHP function, pseudo-code or code snippet that will allow me to do it myself. 我需要知道如何用PHP代码或者至少在PHP函数,伪代码或代码段的方向上进行推入或暗示来复制这一代,这些使我自己可以做到这一点。 Thank you in advance! 先感谢您!

Generate your random numbers at the beginning and save it in a session. 在开始时生成随机数,并将其保存在会话中。 That way the random numbers for that user is always known and you can know what id of question you should go back to by looking it up in the session. 这样,始终可以知道该用户的随机数,并且可以通过在会话中查找来知道应该返回的问题ID。

Cheers 干杯

When the user with ID 10 opens the page for the first time, use rand() to generate random numbers then store them into a cell in the users table in database. 当ID为10的用户首次打开页面时,请使用rand()生成随机数,然后将其存储到数据库中users表的单元格中。 So the user with id 10 has the rand() numbers stored. 因此,标识为10的用户将存储rand()数字。

For example the users table has id, rand_questions. 例如,用户表的ID为rand_questions。 Check if the rand_questions is empty then update with the new random numbers generated, else you get the numbers from the database. 检查rand_questions是否为空,然后使用生成的新随机数进行更新,否则从数据库中获取数字。

you can get random $w array values. 您可以获得随机的$ w数组值。 try this code as example and change with your logic. 尝试将此代码作为示例,并根据您的逻辑进行更改。

$w = array('0'=>11,'1'=>22,'2'=>44,'3'=>55,'4'=>66,'5'=>88);

$str = '';
for($i=0;$i<5;$i++) {
        $str.= $w[rand(0,5)];
}

As this article suggests, you could use a non-repeating pseudo random number generator. 本文所建议,您可以使用非重复的伪随机数生成器。 Only problem would be to generate a primnumber that is atleast 2x as big as the upper-bound for IDs and satisfies the condition p = 3 in the ring Z4. 唯一的问题是生成一个素数,该素数至少是ID上限的2倍,并且满足环Z4中的条件p = 3 Though there should be big-enough primnumbers matching the conditions on the net for free use. 尽管应该有足够大的素数与网络上的条件匹配,以供免费使用。

Due to my lack of experience with PHP i can only provide pseudocode though. 由于缺乏PHP经验,因此我只能提供伪代码。

int[] generateUniqueRands(int id , int ct)
    int[] res

    const int prim//the primnumber described above

    for int i in [0 , ct[
        res[i] = ((id + i) * (id + i)) % prim

    return res

Note that this algorithm basically works like a window: 请注意,此算法基本上像一个窗口一样工作:

id = x      set = [a , b , c , d]
id = x + 1  set = [b , c , d , e]
...

If you wish to avoid this kind of behavior just generate a unique random-number from the id first (can be achieved in the same way the set of random numbers is generated). 如果您想避免这种行为,请先从id生成一个唯一的随机数(可以通过生成随机数集的相同方式来实现)。

Why reinvent the wheel 为什么要重新发明轮子

mt_srand(44);
for ($i=0; $i < 10; $i++) echo mt_rand(). "\n";
echo "\n\n";
mt_srand(44);
for ($i=0; $i < 10; $i++) echo mt_rand(). "\n";

result 结果

362278652
928876241
1914830862
68235862
1599103261
790008503
1366233414
1758526812
771614145
1520717825


362278652
928876241
1914830862
68235862
1599103261
790008503
1366233414
1758526812
771614145
1520717825

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

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