Agil Asadi Agil Asadi - 2 months ago 7
C# Question

Random number generator 8n c#

Now I always had problems with random number generating in other languages. I need to generate 1000 random numbers with 6 digits length but it must have 2 letters before the 6 digites and the letters will stay constant through all numbers. I will be copying these cods to use on some kind of products. Console application is fine at this point as well. All the numbers must be different
Ex:
AB649205
AB749264
AB650302
...

Answer

You could try this:

private void btnSubmit_Click(object sender, EventArgs e)
{
    //clear old data
    txtOutput.Clear();


    //The prefix of the numbers
    String letters = txtLetters.Text;
    if (string.IsNullOrEmpty(letters))
    {
        //returns when the prefix is empty
        MessageBox.Show("Please enter a prefix!");
        return;
    }
    //Random generator
    Random random = new Random();

    //For loop for 1000 times
    for (int i=1; i<=1000; i++)
    {
        //add the int into the output
        int randomInt = random.Next(100000, 999999);
        txtOutput.Text += letters + randomInt + "\r";
    }
}

I hope this helps!

Comments