Search code examples
phpprng

How can I generate unique random numbers in PHP?


I am working on a MCQ module and I need to fetch random questions from my database. The problem is that I seem to get duplicates.


Solution

  • If you're fetching them from database, use SQL to do your job. e.g. fetching 20 random questions (without repeating):

    SELECT * FROM questions ORDER BY RAND() LIMIT 20