how to (even logically) random at leastone 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.
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
answer table.
you could have a staticList<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
fiverandomly chosen question ids.
Step 1: at startup of you ASP.NET application, populate the static List<question id type> with all
question ids.
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.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
szewciu
Member
7 Points
36 Posts
Difficult Random
Jan 06, 2013 11:47 AM|LINK
Hi,
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!
szewciu
Member
7 Points
36 Posts
Re: Difficult Random
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
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Difficult Random
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 answer table.
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 question ids.
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?
g.
http://msdn.microsoft.com/en-us/library/system.random.aspx