1

I want to generate 4 digit random number and put it in the text box as serial number of something . how do I do that and yet to some extent be sure I wouldn't get duplicated numbers?

5
  • 1
    What do you mean by duplicated numbers? Duplicated on your computer? During your program session? All over the world? It's not so easy to prevent duplication on 4 digit numbers... Commented Jul 23, 2013 at 15:30
  • 2
    Is 0010 considered a 4 digit random number for your requirements? Commented Jul 23, 2013 at 15:31
  • What is the scope over which the numbers must not be duplicated? Just on the current display? In all displays for the current run of the program? For all runs of the program? For all runs of the program on all computers? Just for today? What about tomorrow - can you have duplicates between days? Commented Jul 23, 2013 at 15:33
  • 1
    Why not start at 0000, and increment each new number by 1? That way, you can be sure it will be unique (until you hit 10 000 items). Do you really need the serial number to look "random"? Commented Jul 23, 2013 at 15:38
  • 1
    Serial numbers are called serial numbers because they are serial; they start with 1 and then go to 2, 3, 4, 5, 6... in series. Commented Jul 23, 2013 at 17:40

8 Answers 8

4

If you want to be sure that you don't get duplicates use a Guid:

Guid guid = Guid.NewGuid();  // however a Guid is not a 4 digit integer

If you want a random number use Random:

Random rnd = new Random();
int randomInt = rnd.Next(1000, 10000)

But note that you should not create the random instance in a loop because it is seeded with the current time. Otherwise you get repeating values. So either pas the random instance as parameter to this method, use a field/property or use the same instance in a loop which was created outside.

The easiest approach to get unique random numbers is to create new numbers if one already exists.

Sign up to request clarification or add additional context in comments.

8 Comments

Guids are not random as such.
@newStackExchangeInstance He did not say they where, he said they where unique.
I don't think a Guid is a "4 digit random number".
@ScottChamberlain Sure, but you can't compress the whole thing down to 4 digits and keep the uniqueness.
A Guid is not a four-digit number, except if your digits are in base-4294967296.
|
2
List<int> used = new List<int>();
Random random = new Random();

foreach(thing you want to do)
{
    int current = random.Next(1000, 9999);

    while(used.Contains(current))
        current = random.Next(1000, 9999);

    //do something

    used.Add(current);
}

or some similar variation on this to meet your needs

Comments

1

Best way to do it is with the Random class:

Random random = new Random();
int result = random.Next(1000, 10000);

You could also use the RNGCryptoServiceProvider class, which gives you more secure random numbers:

RNGCryptoServiceProvider csp = new RNGCryptoServiceProvider();
byte[] bytes = new byte[4];
csp.GetBytes(bytes);
int yourNum = bytes.Select(x => x % 10).Aggregate(0, (a, b) => (a * 10) + b);

Comments

0

The Random class is used to create random numbers. (Pseudo-random that is of course.)

Example:

Random rnd = new Random();
int num = rnd.Next(1000, 10000); //creates number from 1000 to 9999

You can use HashSet for store generated number and check duplicate

Comments

0

The easiest way to do this is generate a list of all possible choices in order and shuffle it. Then after shuffling the list just go through the list "in order" and the sequence will be random and non repeating.

List<int> numbers = new List<int>(Enumerable<int>.Range(0,10000);

HelperFunctions.Shuffle(numbers); //There are plenty of examples of how to shuffle the list on this site.

foreach(var number in numbers)
{
    Console.WriteLine(number.ToString("D4"); //Displays random numbers from 0000 to 9999 but never repeats
}

Comments

0

Like this:

var random = new Random();

var numbers = new List<int>(0);

for (var i = 0; i < 4; i++)
{
    var number = random.Next(1000, 10000);

    if (numbers.Contains(number))
    {
        i--;
        continue;
    }

    numbers.Add(number);
}

tbx1.Text = numbers[0].ToString();
tbx2.Text = numbers[1].ToString();
tbx3.Text = numbers[2].ToString();
tbx4.Text = numbers[3].ToString();

Comments

0

This will give you a 4 digit random number.

Random random= new Random();   

int RnNum = random.Next(1000,9999);

If you want to be sure that it doesn't get duplicated, then store the generated random values in Session & compare if its already generated. Of course there are other ways, if you don't want to do it the session way, write to a text file or something

Comments

0

You need to keep a record yourself of the numbers that have been generated in Dictionary or Hashset. Like

 HashSet<int> generatedValues = new HashSet<int>();
 Random rnd = new Random();
 int randomInt = rnd.Next(1000,9999);
 while(generatedValues.Contains(randomInt))
 {
    randomInt = rnd.Next(1000,9999);
 }
  generatedValues.Add(randomint);

1 Comment

You can just use a HashSet<T> instead of a Dictionary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.