我有4批次:
10000-30000
30001-50000
50001-70000
71000-99999
我必须为以下组生成500个随机数,不包括{70000-71000}
我的代码有点像这样。我的数据网格显示了500条记录,但它的数字介于{70-71k}之间。我需要计算不能给我所需的数量" 500"算,因为某些随机数在{70-71k}之间生成,我需要排除。
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)
{ }
}
答案 0 :(得分:1)
不是丢弃70000-71000范围内的数字,而是从10000-89999生成一个数字,如果它大于70000,则向其中添加1000。例如:
var num = rnd.Next(10000, 90000);
if (num > 70000)
{
num += 1000;
}
您只是将70001-89999范围内的数字映射到71001-99999。
答案 1 :(得分:0)