Sorry for asking such a vague question, i’m new to using C# / ASP.NET. When creating randomly generated questions, and storing the question objects within a list through the use of a for loop the same / duplicate question is showing up when I output the questions. I’ve inspected the list of questions with the debugger, and it shows 10 of the same object.
Server-side C# Used to populate the list of questions:
private void Populate()
{
__NumberOfQuestions = 10;
__Questions = new List<Question>(__NumberOfQuestions);
for (int i = 0; i < __NumberOfQuestions; i++)
{
__Questions.Add(new Question());
}
QuestionList.DataSource = __Questions;
QuestionList.DataBind();
}
Question class is avaialable here:
http://pastebin.com/mEArQFHh
ASP Code used to add the questions to the page:
<asp:Repeater ID="QuestionList" runat="server">
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem,"__LeftOperand")%></td>
<td><%#DataBinder.Eval(Container.DataItem,"__Operator")%></td>
<td><%#DataBinder.Eval(Container.DataItem,"__RightOperand")%></td>
<td><%#DataBinder.Eval(Container.DataItem,"__Answer")%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
Output:
9
SUBTRACT
6
3
9
SUBTRACT
6
3
9
SUBTRACT
6
3
9
SUBTRACT
6
3
9
SUBTRACT
6
3
9
SUBTRACT
6
3
9
SUBTRACT
6
3
9
SUBTRACT
6
3
9
SUBTRACT
6
3
9
SUBTRACT
6
3
What you’re doing wrong:
Randomobject for eachQuestion.Randomconstructor, the default,time-based, seed is used. This uses
Environment.TickCount(though I don’t think this is documented specifically), so it has millisecond granularity.they’ll use the same seed.
same values.
The simplest way to fix this is to make your
Randomobjectstatic, which means it’s shared by all of theQuestionobjects — or belongs to theQuestionclass, rather than to question instances. This means it’ll only be constructed once, and you’ll get a sensible sequence of random numbers.