Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to random number in loop with 8 digits.
for example:
for (i=0;i<10;i++)
{
//here I want to random number like this :35467382
}

How can I do it?
Posted

Try:
C#
private Random rand = new Random();

private void DoRand()
    {
    for (int i = 0; i < 10; i++)
        {
        int randNo = rand.Next(10000000, 100000000);
        Console.WriteLine(randNo);
        }
    }

The lower bound excludes numbers with leading zeros, the upper bound limits it to 10000000..99999999 inclusive.
 
Share this answer
 
Comments
Member 10304617 16-Oct-13 4:32am    
Thanks!!
OriginalGriff 16-Oct-13 4:45am    
You're welcome!
Use the Random class eight times to give you a number between 0 and 9!
 
Share this answer
 
Here is the below code to generate 8 digit random number code.

C#
Random random = new Random();
            char[] ch = "0123456789".ToCharArray() ;

            for (int j = 0; j < 10; j++)
            {
                string random_number_string = "";
                for (int i = 0;random_number_string.Length!=8; i++)
                {
                    int x = random.Next(0, ch.Length);
                    random_number_string += ch.GetValue(x).ToString();
                    
                }
                Console.WriteLine(random_number_string);
                
            }
 
Share this answer
 
Comments
Member 10304617 16-Oct-13 4:49am    
Thanks you!
Ron23111 25-Aug-18 20:35pm    
And if you also want it each number to star with 18 like 18126342, 18436253, 18627536 etc..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900