I have 4 batches:
10000-30000
30001-50000
50001-70000
71000-99999
I have to generate 500 random numbers for the followings groups excluding the values from {70000-71000}
My code is somewhat like this. My datagrid shows 500 records but it has numbers between {70-71k}.I need to calculate the count which is not giving me the required "500" count because certain random numbers are getting generated between {70-71k} which i need to exclude.
int i=10000, j=99999;
int batch1Count = 0, batch2Count = 0, batch3Count = 0, batch4Count = 0;
private void button1_Click(object sender, EventArgs e)
{
try
{
this.dataGridView1.Rows.Clear();
string prefix = comboBox1.SelectedValue.ToString();
switch (prefix)
{
case "1": prefix = "USN"; break;
case "2": prefix = "DOT"; break;
case "3": prefix = "USAF"; break;
case "4": prefix = "COV"; break;
}
for (int k = 0; k < 500; k++)
{
Random random = new Random(DateTime.UtcNow.Millisecond);
//Generate random values
int result = random.Next(i,j);
Thread.Sleep(1);
if (result >= 70000 && result <= 71000)
{
k=k-1;
}
else
{
//Add prefix to the generated random value
string key = prefix + result;
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dataGridView1);
row.Cells[0].Value = key;
this.dataGridView1.Rows.Add(row);
Thread.Sleep(3);
if (result >= 10000 && result <= 30000)
{
Thread.Sleep(1);
batch1Count++;
}
else if (result >= 30000 && result <= 50000)
{
Thread.Sleep(1);
batch2Count++;
}
else if (result >= 50000 && result <= 70000)
{
Thread.Sleep(1);
batch3Count++;
}
else if (result >= 71000 && result <= 99999)
{
Thread.Sleep(1);
batch4Count++;
}
}
}
}
catch (Exception ex)
{ }
}