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?
- 1What 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...Cédric Bignon– Cédric Bignon2013-07-23 15:30:41 +00:00CommentedJul 23, 2013 at 15:30
- 2Is 0010 considered a 4 digit random number for your requirements?Cemafor– Cemafor2013-07-23 15:31:32 +00:00CommentedJul 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?Matthew Watson– Matthew Watson2013-07-23 15:33:17 +00:00CommentedJul 23, 2013 at 15:33
- 1Why 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"?Pierre-Luc Pineault– Pierre-Luc Pineault2013-07-23 15:38:49 +00:00CommentedJul 23, 2013 at 15:38
- 1Serial numbers are called serial numbers because they areserial; they start with 1 and then go to 2, 3, 4, 5, 6... inseries.Eric Lippert– Eric Lippert2013-07-23 17:40:13 +00:00CommentedJul 23, 2013 at 17:40
8 Answers8
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 integerIf 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.
8 Comments
Guids are not random as such.Guid is not a four-digit number, except if your digits are in base-4294967296.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
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);Comments
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 9999You can use HashSet for store generated number and check duplicate
Comments
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
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
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
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
HashSet<T> instead of aDictionaryExplore related questions
See similar questions with these tags.



