Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 06, 2013 03:51 PM by gerrylowry
Jan 06, 2013 11:47 AM|LINK
how to (even logically) random at least one e.g. Id from database which has specified column value?
I mean I have in database table id, question id, answer text value and bool (if answer is correct) columns and I want to get e.g. three answers Ids, that (at least)
one of them is correct.
Many thanks for your help in advice!
Jan 06, 2013 11:56 AM|LINK
I know I have to check how many correct answers the question has.
Then I need to random how many correct answers to display.
And I want to know how to mix these correct answers with incorrect.
E.g. randomize 5 questions, for each question randomize 3 answers, that random num of them (but at least one) is correct
Jan 06, 2013 03:51 PM|LINK
@ szewciu welcome to forums.asp.net
TIMTOWTDI =. there is more than one way to do it
if i understand your schema correctly, you would have more than one table:
questions with question id,
answers with question id as a FK (foreign key) in the
you could have a static List<question id type> that contains all
question ids from the question id table.
you could have a non-static List<question id type> to contain
five randomly chosen question ids.
Step 1: at startup of you ASP.NET application, populate the static List<question id type> with all
Step 2: for each instance where you need to generate a list of question ids create an empty non-static list (new), generate five or more pseudo-random numbers from 0 to one less than then number of your
question ids in your static list. Test that your pseudo-random number is not already in the non-static list before adding it; stop when you have your five questions.
Step 3: for each question id in your non-static list of questions, create one non-static list of
answer ids; where the FK of the answer corresponds to the
question id; from the non-static list of answer ids that correspond to your
question id, using a technique that is similar to Step 2, grab your three pseudo-random
answer id's; iterate (foreach) through your non-static List<answer id> to ensure that you have a least one marked correct ... if you fail, keep regenerating that list until you have a least one marked correct.
Note: solving a problem like this is similar to solving it manually with pen and paper ... the only difference is that you're translating your solution into c# code.
BTW, is this a homework assignment?