English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I want to display the questions randomly from the database (ie in online quiz program the question will be displayed randomly to students)

2007-03-20 02:57:48 · 4 answers · asked by Saravanan R 1 in Computers & Internet Programming & Design

4 answers

Get all the primary keys (e.g. IDs) of the questions in a list. and then select random indexes from this list by using a random seed (it depends on your programming language). When you pick a random index, get the primary key that is stored in it & then get its corresponding question.
In this way you will always ensure not to pick an ID that does not exist.

2007-03-20 03:04:51 · answer #1 · answered by ramy e 2 · 0 0

What SQL software are you using?

Whatever it is, check to see if it has a RAND (random number) function, then use it to randomly pick from the available IDs.

For example, you could first assign a variable to the total number of question records in the Questions table. Then, perform a SELECT where the ID of the question equals the result of the RAND function. This should randomly pick an ID from among the available question IDs.

2007-03-20 03:06:01 · answer #2 · answered by Anonymous · 0 0

You questions need to have a numeric sequence key.
You can find the max(key) from the table so now get your program to generate a random key from 1 to max(key).
If you don't want duplicates then this is another problem.
If you have gaps in the sequence then you will just have to get another random number if nothing found.

2007-03-20 03:04:31 · answer #3 · answered by AnalProgrammer 7 · 1 0

I work on MS-SQL server & this is how I do it

SELECT TOP 10 * FROM someTable ORDER By NEWID()

It selects 10 random records!

Quite charming isn't it?

2007-03-20 08:28:38 · answer #4 · answered by Smutty 6 · 0 0

fedest.com, questions and answers