Click here to Skip to main content
15,886,638 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am Developing a on-line test application.
my requirements are-

1. Questions will be retrieved from database randomly.
2. Options are also shuffled if i start the test again.
3. One question at a time is displayed on the page and on next button click next question is displayed.

can any one give me the solution for that-
1. how to display one question at a time on the page
2. how to shuffle options.


thanks in advance
Posted
Comments
George Jonsson 3-Oct-14 6:26am    
If you have an integer ID for your question, you can use the Random class to get a number between you min and max IDs in your database table.
For how to display the question, there are tons of samples. Just search with your favorite engine.
Sinisa Hajnal 3-Oct-14 6:32am    
Get whole test at once, that way you avoid having to watch for duplicates. You can store it in cache/cookie/session/however you see fit.

Option - retrieve them already shuffled. Good option for SQL Server is ORDER BY NEWID() which will sort the options by random generated GUIDs

In order to extract randomly your questions you have to provide an ordering criterion to your table of questions, a auto-increment field (say id) would fit very well.
Then you could use the random number generator (Random class) for extracting a random number and use it in your query:
SQL
select question from question_table where id=@random_number

If you don't want question repetitions (that is selecting two or more times the same question in the same test) then have a look at the algorithm shown in my tip: "Random extraction of 5 cards from a deck"[^].
 
Share this answer
 
You get minimun and maximum value of question Id (minQId and maxQId) from database and choose a random ID as:
C#
Random rnd = new Random();
int questionIdToFetch = rnd.Next(minQId, maxQId); 

Then get that complete question and display.

To suffle asnwers below link would be useful:
http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp[^]

Let me know if there is any further question.
 
Share this answer
 
How to shuffle options on each get

SQL
SELECT * FROM OPTIONS 
WHERE question = @your_question
ORDER BY NEWID()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900