-9

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)
        { }
    }
  • 1
    Your _statement_ should actually be a _question_, with a clear evidence that you tried something before "asking" for help. – Andrei V Jun 18 '14 at 06:12
  • Welcome to Stack Overflow. This is not a good way to ask a question here. Did you try _anything_ so far to solve your problem? Show your effort first so people might show theirs. Please read [FAQ], [ask] and [help] as a start.. – Soner Gönül Jun 18 '14 at 06:12
  • This will help you http://stackoverflow.com/questions/2706500/how-to-generate-random-int-number-c – Midhun Murali Jun 18 '14 at 06:13
  • "Random" means possibly including duplicates (throwing dice), so for the first batch for example, you might end up with `{20000, 20000, 20000, 20000, ...}`. Are you really sure you want that? I'm just guessing, but you probably want to put all possible values in a bag and then randomly take out one number after the other, until you have enough numbers. – Corak Jun 18 '14 at 06:23

2 Answers2

1

Can't you just use the Random class for it?

The Random class has a Next(int, int) method that generates a random number between the specified values. You could simply generate one and if it is in the {70k-71k} range, generate another one.

Nahuel Ianni
  • 3,177
  • 4
  • 23
  • 30
  • My code is somewhat like this. I want 500 records not in the range of {70-71k} – user3751035 Jun 19 '14 at 03:42
  • Create a loop and for each iteration, generate a new Random number (before moving to the next iteration, make sure the newly generated number is not on the undesired range). – Nahuel Ianni Jun 19 '14 at 06:00
  • Thanks man. What I tried to do is if the random number falls in the excluded range i reduced the incrementer by 1 and re ran the loop again. Now my datagrid has perfect 500 records. But the categories sum total count which i am trying to do is always between 400-500 – user3751035 Jun 19 '14 at 10:53
  • Glad it helped, just be sure to use only one instance of random (instead of "new Random().Next(...)", it will be better for performance and results :) – Nahuel Ianni Jun 19 '14 at 11:03
1

Rather than throwing out numbers that are in the range 70000-71000, generate a number from 10000-89999 and if it's greater than 70000 then add 1000 to it. For example:

var num = rnd.Next(10000, 90000);
if (num > 70000)
{
    num += 1000;
}

You're just mapping the numbers in the range 70001-89999 to 71001-99999.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351