I have to generate a set of numbers consisting of 5 digits, as shown below. I tried to search some info about this but found nothing.
int playerId;
System.Random rand = new System.random();
for (int i = 0; i <= 5; i )
{
playerId = random.Next();
}
Console.Write($"Generating player ID: {playerId}");
// output example: Generating player ID: 1158453178
As a result, i got a set of numbers consisting of 10 digits. How can i solve this problem?
CodePudding user response:
you should put Console.Write($"Generating player ID: {playerId}");
inside the for block, something like this:
int playerId;
System.Random rand = new System.random();
for (int i = 0; i < 5; i )
{
playerId = random.Next();
Console.Write($"Generating player ID: {playerId}");
}
CodePudding user response:
What you're doing in your code is generating five different random numbers of 10 digits (random.Next()
generates integers less than the integer maximum value, 2147483647) and printing the last one. If you want to generate a 5 digit random number, you can just use random.Next(100000).ToString("D5")
, which will generate a random positive integer under 100,000, and then fill out the front with zeroes if needed.