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:00Commented Jul 23, 2013 at 15:30
-
2Is 0010 considered a 4 digit random number for your requirements?Cemafor– Cemafor2013-07-23 15:31:32 +00:00Commented 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?Matthew Watson– Matthew Watson2013-07-23 15:33:17 +00:00Commented Jul 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 you really need the serial number to look "random"?Pierre-Luc Pineault– Pierre-Luc Pineault2013-07-23 15:38:49 +00:00Commented Jul 23, 2013 at 15:38
-
1Serial numbers are called serial numbers because they are serial; they start with 1 and then go to 2, 3, 4, 5, 6... in series.Eric Lippert– Eric Lippert2013-07-23 17:40:13 +00:00Commented Jul 23, 2013 at 17:40
8 Answers
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.
8 Comments
Guids are not random as such.Guid is not a four-digit number, except if your digits are in base-4294967296.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
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 a Dictionary