开发者

Random question in question paper without repetition in C#

开发者 https://www.devze.com 2023-02-22 05:56 出处:网络
I\'m going to develop a project in asp.net with C#. The project is an online examination system. The problem is, how will I select the random question in a certain time period from a database? The dat

I'm going to develop a project in asp.net with C#. The project is an online examination system. The problem is, how will I select the random question in a certain time period from a database? The database I'm usin开发者_开发问答g is SQL Server 2005. Please give all solutions in C#.


Taking a shot in the dark here

Assuming you have a table with questions, each (hopefully) with an "id" column:

You could go:

1.) Get the total number of questions

2.) Randomly pick the id of a question to be selected from the database

Something like

public Question GetRandomQuestion() 
{
    Random r = new Random();

    int totalNoOfQuestions = GetTotalNoOfQuestions(); //get this from database

    int questionIdToPick = r.Next(totalNoOfQuestions);

    return GetQuestion(questionIdToPick); //some method to fetch from database
}
0

精彩评论

暂无评论...
验证码 换一张
取 消