is there any good method to generate random even numbers with C#? I came up with this code:
Random RandString = new Random(0);
code:
private void button6_Click(object sender, EventArgs e)
{
int min = 0;
int max = 50;
int rand = RandString.Next(min, max-1);
while (rand % 2 == 1) // odd
{
rand = RandString.Next(min, max-1);
}
textBox4.Text = "" + rand;
}
Problems with this code:
- my current solution might not be the fastest of all
- very often, numbers that are generated by this method are repeated with the same sequence! For example after 5 iterations I can get: 8, 10, 20, 30, 16. After more clicking (for like 15 seconds) I can get 10, 30, 20 in a row or 30, 20, 10.. I’m not sure, but that’s NOT what I call “random”.
so is there any way to deal with the problem that I’ve described, or is there any other method?
1 Answer