c# - Random number distribution is uneven / non uniform -
I have seen a strange issue with the random number generation in C #, it seems that the pattern is too much Repeatedly often you expect.
I am writing a system that generates the activation code, a series of 7 numbers (range 0-29). Mathematics must be 30 ^ 7 (22billion) in possible combinations of activation code. Based on this, 1 billion codes should not be expected to obtain a duplicate activation code before it is generated. Although my examination is underway, I start getting duplicate codes after about 60,000 repetitions, which is very surprising. I also tried to use RNGCryptoServiceProvider with the same results, I get duplicates at around 100,000 iterations.
I really have to know whether it is the random number generation bug in the net /
fixed zero main (string [] args) {random random ====== ================================================== === I New Random (); Random Activation Code (Rand, True); Console.Out.WriteLine (press "Enter"); Console.ReadLine (); } Fixed Zero random activationcode (random random generator) {var maxItems = 11000000; Var list = new list & lt; String & gt; (MaxItems); Var activation code = new hashset & lt; String & gt; (List); ActivationCodes.Clear (); Date Time Start = DateTime.Now; (Int i = 0; i & lt; maxItems; ++ i) {string activationCode = ""; {ActivationCode + = randomGenerator.Next (0,30) + "-"; for (Int j = 0; j & lt; 7; ++); } If (activationcode.resources (activation code) (console.aut.lineline ("code:" + activation code); Console.Out.WriteLine ("duplicate on the go:" + i.ToString ("##, #") ); Console.Out.WriteLine (press "Enter"); Console.ReadLine (); Console.Out.WriteLine (); Console.Out.WriteLine ();} Else {activationCodes.Add (Activation Code);} If ( I% 100000 == 0) {Console.Out.WriteLine ("Change:" + i.ToString ("##, #"); Console.Out.WriteLine ("Time Elapsed:" + (Date Time.Now - Start));}}}
My work is to use the number 10 activation code, which means that the test is without any duplicate values The trial runs up to 11 million iterations (after point, it runs out of memory).
It is not surprising, this is what you should expect. Your impression is that when the scope of the possibilities is large, then it should take a long time to generate duplicates, is just false, therefore believe Stop Acting Start believing in the truth: If you Bvta possible code, you should duplicate the root encoded, which is approximately 150 thousand if n 22 billion.
Think about it the way: When you generate Root-N code, most of them had a Root-En-in-N chance of a collision Root-N- Multiply root-n in in-n, and you get ... about 100% chance of collision.
This is not strict logic, but you should give correct motivation to change your faulty belief. If this argument is impossible, then you will want to read your article on this topic:
If you want to create exclusive code then generate the GUID ; That's what they are Note that the GUID is not guaranteed to be random, it is guaranteed to be unique only .
Another option to generate random is actually all random, but unique, number 1, 2, 3, 4, ... generated as much as you want And then use qualitative inverse techniques to create a random-looking unique encoding of those numbers. See for details.
Comments
Post a Comment