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?
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.
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}");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.