samedi 1 août 2015

PHP - Generating random integers within specified range from a key

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: http://ift.tt/1IB7TTC 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!




Aucun commentaire:

Enregistrer un commentaire