tags:

views:

1786

answers:

7

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.

A: 

What methods have you tried? I don't know if any RNG can guarantee non-repeating numbers in theory, but you can always store a table of results and throw away any repetitions based on that.

Peter
Be careful doing something like this. If you are generating truly random numbers, there will *eventually* be repetitions. Discarding the repetitions makes the process unrandom.
theycallmemorty
I agree--it would make things less random; in his case though, with his aim that he fetch random questions, it makes sense to view the problem as something like a raffle. He's drawing random numbers from a hat: once one is drawn, it can't be drawn again.
Peter
i hv first produces the random no. by mt_rand() and then store them in a string for further reference and check each time for the no. compiaring with previous values.......but the repitiotion test fails dome time......
PROFESSOR
This kind of stuff should be a comment on his question, not an answer.
TravisO
A: 

Without any more info i can suggest a rudimentary solution. (but please update your question with more info)

I'm guessing you have users, because then you could save into a table (be it temporary or not), what questions said user has already gotten.

If you don't have users, you can use the SESSION_ID as a user identifier for that user.

So when you fetch a question for the first time, and the user answers it, it saves the info you need to save, and then the user's id and the question's id into a table.

When fetching the next question, you do a check to see if the user has that question id in this new table.

Ólafur Waage
+6  A: 

Sounds like you want to shuffle the questions, not randomize access to them. So your algorithm would be something like this.

  1. Get the all question (or question keys) you want to display.
  2. Shuffle them
  3. Retrieve/ display in them in the shuffled order

for shuffling check out: Fisher-Yates shuffle algorithm

Greg Dean
+1  A: 

If you're using MySql and you have reasonable small amount of data, you can use ORDER BY RAND()

soulmerge
Other RDBMSes have similar syntax. For example, I believe you can use "ORDER BY NEWID()" in MSSQL and "ORDER BY DBMS_CRYPTO.randominteger" in Oracle.
Ben Blank
A: 

See http://stackoverflow.com/questions/203382/do-stateless-random-number-generators-exist

Any sequence of pseudo-random numbers will eventually repeat. How you obtain your pseudo-random numbers?

cdonner
+5  A: 

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
vartec
A: 

If you have a very large number of rows you can add a column to the table which stores a number between 0 and 1 and then fetch with a query:

SELECT * FROM `mytable` WHERE `randcolumn` > RAND() LIMIT 20

This means that your database doesn't have to randomly order the entire table to provide just 20 rows.

David Caunt