简体   繁体   中英

C# needing a six digit number beginning with two 0's

I'm making a program where products are searched for by there batch number. I want to the batch number to be a random 6 digit code starting with 00, for example 002142; at the moment it is only making a random 6 digit code. Here is the code I have tried:

 public string getRandomNumber()
    {
        Random random = new Random(100000);
        string randomNumber = random.Next(999999).ToString("D6");
        return randomNumber;
    }
public string getRandomNumber()
{
    Random random = new Random();
    string randomNumber = "00"+random.Next(10000).ToString("D4");
    return randomNumber;
}

You should not set a fix seed in the Random, because it will allways generate the same random number sequence. For example if you call your function in a loop it will allways return the same number, thus not being random.

Also the parameter in the Next() function is the upper bound exclusive so use 10000 instead 9999 so you can get 9999 as a random number as well.

why is everybody creating 6 digit ? what really is needed is create 4 and concat.

int random = new Random().Next(1000, 9999);
string code = "00" + random.ToString();

EDIT:

Thanks for marking as correct answer but my code is wrong. If you don't pass minimum value to next method, you can get numbers from 1 to 999 which you don't want. I edited my answer. Hope it's not late for anything.

You wrote .Next(999999) so i assume you want random values from 0 to 999999.

First you should write 1000000 because .Next() determines a random number from 0 to exclusive the input value.

Then you should

replace

string randomNumber = random.Next(1000000).ToString("D6");

with

string randomNumber = random.Next(999999).ToString().PadLeft(6, '0');

to fill the missing digits with 0

Another possibility is "000000" format string ( six digits, leading zeros are mandatory):

  • random.Next(10000) - last 4 digits are random
  • ToString("000000") - 6 digits are returned (at least 2 leading zeros)

Implementation

 string randomNumber = random.Next(10000).ToString("000000");

Another issue is that you should not declare random locally:

 // Simplest, but not thread safe
 private static random = new Random();

 ...

 public static string getRandomNumber()
 {
     return random.Next(10000).ToString("000000");
 }

You were close. What you really want is a random four digit number, padded with two leading zeroes (to six places) . So use Random.Next(10000).ToString("D6") or . ToString("000000") . ToString("000000")
Note however for numbers less than 1000, you will end up with more than two leading zeroes. To avoid that, you can do Random.Next(1000,10000) where the first number is inclusive, and the second exclusive.

Note also that the way you are initializing the random is incorrect if you are going to be calling this method more than once! Make your Random object a class member (preferably static ) and only seed/initialize it once (don't pass 100000 to its constructor) Otherwise if this method is called more than once, it is going to return the same value each time.

Use new Random() . If you are going to seed it the way you do, it must be a static variable (or at least a class member--however for each instance of the class you will end up with the same values being generated)

I would do something like this:

var rnd = new Random(); // the internal seed is good enough
var rndNums = string.Join("", Enumerable.Range(0, 4).Select(x => rnd.Next(10)));
return "00" + rndNums;

Then you can easily change the amount you want, like this:

string GetRandomBatch(int numberOfRandomNumbers)
{
    var rnd = new Random(); // the internal seed is good enough
    var rndNums = string.Join("", Enumerable.Range(0, numberOfRandomNumbers).Select(x => rnd.Next(0, 9)));
    return "00" + rndNums;
}

With interpolated string (C# 6.0) you can do it like this :

Random random = new Random();
string randomNumber = $"00{random.Next(9999)}";

Your ToString("D6") does exactly what it appears that you want, but since you are passing a large upper bound for your random.Next , there will in many scenarios not be anything to pad.

As others have indicated, you may prefix "00" , or you could simply set a lower upper bound, since the "D6" will take care of your padding:

string randomNumber = random.Next(9999).ToString("D6");

If you want the number to always be 4 digits and not, say, "000123" , you'll need to specify a lower bound as well:

string randomNumber = random.Next(1000, 9999).ToString("D6");

Also note that if you are specifying a hardcoded seed as in your example, you will always get the same random number back.

Something like this will do.

public static void Main()
{
    Random rnd = new Random();
    string Value = rnd.Next(1, 9999).ToString("D6");
    Console.WriteLine(Value);
}

Or with lower possibility of getting the same number would be

public static void Main()
{
    Random rnd = new Random(DateTime.Now.Millisecond);
    string Value = rnd.Next(1, 9999).ToString("D6");
    Console.WriteLine(Value);
}
public string RandomNum()
{
    return "00"+ new Random().Next(9999).ToString("D4");
}

you can also give minimum and maximum number to Next() function. Like:

return "00"+ new Random().Next(1,9999).ToString("D4");

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM