During the development time，You will inevitably use a random number。And still more when，We need to generate a series of random numbers numeric string spare。Fortunately, however, a variety of programming language provides a simple and easy to use pseudo-random number generator for your use，For example, in the C # **Random**。

but，C # is the default system clock Random seed - this method is simple and crude，Unfortunately, once the case a short time generate a lot of random encounters on the stretched - a bunch of the same random number to jump out，But precisely because the random，It will have the same number appears，But for us in terms of the human senses，"Random" actually means "*Random non-repeating*”。

So，You need a**Deduplication**Algorithm。simply speaking，Use collection type is the best solution，For example, I am familiar with Swift has had a
Set Types of，It automatically filters out duplicate elements ...... but I am not familiar with C # in a similar type，I also used the more popular online methods。

Internet more popular method is recursive way to weight，Another is to use Hashtable 。I forgot there is a，I use the latter，This is easy to understand Ye implement，Simple logic。

## Generates a random number

Probably the idea is that we got a random number，Just add it into the Hashtable ，Since this is a dictionary type，Then we put key and value They are set to the same value - the string that is to be saved。Next, each of the strings have to be stored before a judgment whether there has been enough，and Hashtable There comes this method .ContainsValue() 。

Then，Code should be like this：

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public Hashtable genRandom(int Number) { Random ran = new Random(); Hashtable myRandom = new Hashtable(); while(myRandom.Count < Number) { int nValue = ran.Next(); if (!myRandom.ContainsValue(nValue)) { myRandom.Add(nValue, nValue); } } return myRandom; } |

Such，We simply pass the required number of random numbers，The genRandom(int) Returns will not be repeated sequence of random numbers with a corresponding number of the，You can easily put Hashtable Into an array。

but，It still is not ok，Such returns different random number length，Both descending，If we need is a period of the same length as the data it? Random number generation from zero to tens of hundreds of thousands，Obviously afterwards slowly picked a bad idea。

## Predetermined area

For example, specific to my own case，I need some same-digit random number - Okay，We also need a filter。What is a filter it? It is actually a limit，We need to implement their own algorithms，because .Next() The method has been given a heavy load，use ran.Next(minValue,maxValue).，This can be achieved so that the random number "wherever" the，it is good，Now we come to the iterative code above：

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public Hashtable genRandom(int Number,int length) { Random ran = new Random(); Hashtable myRandom = new Hashtable(); int lowLine = 1; for (int i = 1;i < length;++i) { lowLine *= 10; } int hightLine = lowLine * 10 - 1; while(myRandom.Count < Number) { int nValue = ran.Next(lowLine,hightLine); if (!myRandom.ContainsValue(nValue)) { myRandom.Add(nValue, nValue); } } return myRandom; } |

First, please forgive me in two properties nomenclature，In short，We can give
genRandom(int,int) Incoming required number of random numbers and a random number for each length*(That is, number of bits)*The。Such，We get a given number、Given the length of the random number sequence。

but，For example, still my case，Here I demand thousands of results - even said that tens of thousands? In short，Although the mechanism here is that we have to go heavy，But duplication is unavoidable need to judge，If the event is repeated，Need to loop again，In this case it will still consume some resources - although the top of the days also matter of seconds*(In fact, also the slowest twelve seconds)*，But you want，The user clicks "**Generates a random number**"Button，Then your application interface suspended animation 2 seconds - this is not called poor user experience - called**bug**。

So，At this time you need a more advanced random number implementation - of course，Compared to this，I think you should be a greater need for more advanced seed。

## Improve randomness

We make the best of use of the system clock，In each cycle time as a seed passed Random examples，This greatly reduces the probability of repetition，Avoid the cycle times do not get a valid number。

1 2 |
long tick = DateTime.Now.Ticks; Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)); |

Replace this code to upper code segment which can be。

In fact, there is another call some encryption method in the random number generator to generate the seed，But I am not too fond of，But also as a method call to generate a random seed? But not as to the reality of local materials。

## One more thing

Here I mention a，I use this method on top of，Use the constructor will return Hashtable Converted to ArrayList ：

1 |
ArrayList myRandomNumbers = new ArrayList(genRandom(5000,13).Values); |

Original article written by Gerber drop-off：R0uter's Blog » A random number in C #

Reproduced Please keep the source and description link：https://www.logcg.com/archives/1347.html