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?

askedJul 23, 2013 at 15:22
saadat rahimi's user avatar
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...CommentedJul 23, 2013 at 15:30
  • 2
    Is 0010 considered a 4 digit random number for your requirements?CommentedJul 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?CommentedJul 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 youreally need the serial number to look "random"?CommentedJul 23, 2013 at 15:38
  • 1
    Serial numbers are called serial numbers because they areserial; they start with 1 and then go to 2, 3, 4, 5, 6... inseries.CommentedJul 23, 2013 at 17:40

8 Answers8

4

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

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

If you want a random number useRandom:

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.

answeredJul 23, 2013 at 15:26
Tim Schmelter's user avatar
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.
AGuid 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

answeredJul 23, 2013 at 15:29
Jonesopolis's user avatar

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 theRNGCryptoServiceProvider 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);
answeredJul 23, 2013 at 15:28
It'sNotALie.'s user avatar

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

answeredJul 23, 2013 at 15:29
Daniil Grankin's user avatar

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}
answeredJul 23, 2013 at 15:29
Scott Chamberlain's user avatar

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();
answeredJul 23, 2013 at 15:29
Jonny Piazzi's user avatar

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

answeredJul 23, 2013 at 15:30
Venkata Krishna's user avatar

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);
answeredJul 23, 2013 at 15:30
Ehsan's user avatar

1 Comment

You can just use aHashSet<T> instead of aDictionary

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.