-2

I am trying to develop a Computer Base Test (CBT) software on PHP.

I want it to retrieve question from MYSQL DATABASE randomly where I have 100 questions in the database.

But using <? rand(1,100); ?> there is real assurance that it may select same question at same session.

How do I select a unique random number in a session?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109

2 Answers2

0

You can use RAND() to get random questions from database. And to take-care of uniqueness of questions you can use IN keyword while selecting new question from database. Means you need to keep track of all old Questions to check IN.

OR you can get all unique questions from database using RAND() at once and use those questions for that session.

AkshayP
  • 2,141
  • 2
  • 18
  • 27
0

I don't know this solves your problem with php. But with Mysql rand() you can retrieve random questions from db.

SELECT * FROM yourtable ORDER BY RAND();

If you want 10 random rows out of 100 rows

SELECT * FROM yourtable ORDER BY RAND() LIMIT 10;
fortune
  • 3,361
  • 1
  • 20
  • 30