1

I have a set of questions with unique IDs in a MySQL database. Users also have a unique ID and are to answer these questions and their answers are saved in the database.

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. 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. 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.

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.

So, I need to generate 'X' unique random integers, within specified range 'Y' that are generated based on supplied integer 'Z'. I should be able to call a function with 'Z' as parameter and receive back the same 'X' integers every time.

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. Thank you in advance!

6
  • did you try rand() function in php? look at here: w3schools.com/php/func_math_rand.asp Commented Aug 1, 2015 at 11:57
  • Yes I did, but the rand functions in PHP that I checked do not accept any "key" to generate their values from and every time they are executed a different set of values is generated. Commented Aug 1, 2015 at 12:06
  • What you meant by "key"? Commented Aug 1, 2015 at 12:08
  • He wants a random number that is predictable and that is contradictory. Commented Aug 1, 2015 at 12:12
  • @Rolle in your case, i would use database to save a single session and questionos id of that session for each user. In that case you just generate random key once and dont have to think for later use. Commented Aug 1, 2015 at 12:12

5 Answers 5

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is perfect. I probably haven't found this PHP function before because I used a word "key" instead of "seed".
0

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.

Cheers

Comments

0

you can get random $w array values. 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)];
}

Comments

0

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. 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.

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).

1 Comment

He Paul, I got it, deleted my answer. Basically my suggestion of md5() is a complicated and maybe an unreliable version of what mt_srand() does.
0

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. So the user with id 10 has the rand() numbers stored.

For example the users table has 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.