user601367 user601367 - 1 month ago 20
C# Question

How to avoid number repetition

Hashtable values = new Hashtable();

protected void Button1_Click(object sender, EventArgs e)
{
Random r = new Random();
int newval = r.Next(10);

values.Add("key",newval);


//foreach (object value in values.Values)
//{
// Response.Write(value.ToString());
//}


ArrayList arrayList = new ArrayList(values.Values);
foreach (int key in arrayList)
{
Response.Write(key);
}
}

Answer

It's random. By definition there exists a statistical possibility that numbers will repeat. It's possible that you can add 10 random integers from 1-10 to a list and that they wouldn't repeat, but it would be unlikely.

Ideally, each time you call a randomization function, you have exactly the same odds of getting any one number as you do of getting any other number, including the number you got last time.

Comments