Use C # To generate random numbers that are not repeated

Source: Internet
Author: User
Tags random seed
As we all know about random numbers, computers cannot generate completely random numbers. The so-called random number generator uses a certain algorithm to perform complex operations on the pre-selected random seeds, use the generated results to simulate a completely random number, which is called a pseudo-random number. A pseudo-random number is selected from a finite number with the same probability. The selected number is not completely random, but from a practical point of view, the random degree is enough. The selection of pseudo-random numbers starts with the random seed. To ensure that each pseudo-random number obtained is "random" enough, the selection of Random Seed is very important. If the random seed is the same, the random number generated by the same random number generator will also be the same. Generally, we use parameters related to system time as random seeds, which is also the default method used by the random number generator in. net Framework.
We can initialize a random number generator in two ways:
The first method does not specify the random seed. The system automatically selects the current time as the Random Seed:
Random ro = new Random ();
The second method can specify an int type parameter as a random seed:
Int iSeed = 10;
Random ro = new Random (10 );
Long tick = DateTime. Now. Ticks;
Random ran = new Random (int) (tick & 0 xffffffffL) | (int) (tick> 32 ));
This ensures that 99% is not the same.
Then, we can use this Random class object to generate a Random number. At this time, we need to use the Random. Next () method. This method is quite flexible. You can even specify the upper and lower limits of the generated random number.
If the upper and lower limits are not specified, use the following:
Int iResult;
IResult = ro. Next ();
The following code returns a random number less than 100:
Int iResult;
Int iUp = 100;
IResult = ro. Next (iUp );
The following code specifies that the returned value must be within the range of 50:
Int iResult;
Int iUp = 100;
Int iDown = 50;
IResult = ro. Next (iDown, iUp );
In addition to the Random. Next () method, the Random class also provides the Random. NextDouble () method to generate a Random double-precision floating point number ranging from 0.0 to 1.0:
Double dResult;
DResult = ro. NextDouble ();
However, if you use the Random class to generate question numbers, there will be duplicates. It is very difficult to generate non-duplicated questions, especially in a small number of questions. I have referred to some methods on the Internet, there are two types: one is to use random seeds to make each Random Seed different to ensure non-repetition; the other is to use some data structures and algorithms. The following describes several methods in the second type.
   
Method 1: The idea is to use an array to save the index number. First, a random array location is generated, and then the index number at this location is obtained, copy the last index number to the current array location, and then reduce the upper limit of the random number by one. For example, first place the 100 number in a number group, each time a random location (the first time is 1-100, the second time is 1-99 ,...), replace the number at this position with the last number.

Int [] index = new int [15];
For (int I = 0; I <15; I ++)
Index = I;
Random r = new Random ();
// Stores the number of randomly generated records that are not repeated.
Int [] result = new int [10];
Int site = 15; // set the lower limit
Int id;
For (int j = 0; j <10; j ++)
{
Id = r. Next (1, site-1 );
// Retrieve a random number and save it to the result array.
Result [j] = index [id];
// Copy the last number to the current position
Index [id] = index [site-1];
// Reduce the minimum position by one
Site --;
}

Method 2: Use Hashtable. [NextPage]

Hashtable hashtable = new Hashtable ();
Random rm = new Random ();
Int RmNum = 10;
For (int I = 0; hashtable. Count <RmNum; I ++)
{
Int nValue = rm. Next (100 );
If (! Hashtable. ContainsValue (nValue) & nValue! = 0)
{
Hashtable. Add (nValue, nValue );
Console. WriteLine (nValue. ToString ());
}
}

Method 3: recursion. It is used to check whether the generated random number is repeated. If the obtained number and the obtained number are repeated, the random number is obtained again.

Random ra = new Random (unchecked (int) DateTime. Now. Ticks ));
Int [] arrNum = new int [10];
Int tmp = 0;
Int minValue = 1;
Int maxValue = 10;
For (int I = 0; I <10; I ++)
{
Tmp = ra. Next (minValue, maxValue); // random count
ArrNum = getNum (arrNum, tmp, minValue, maxValue, ra); // The retrieved value is assigned to the array.
}
.........
.........
Public int getNum (int [] arrNum, int tmp, int minValue, int maxValue, Random ra)
{
Int n = 0;
While (n <= arrNum. Length-1)
{
If (arrNum [n] = tmp) // use loops to determine whether there are duplicates.
{
Tmp = ra. Next (minValue, maxValue); // obtain the result randomly.
GetNum (arrNum, tmp, minValue, maxValue, ra); // recursion: If the obtained number is repeated with the obtained number, it is randomly retrieved.
}
N ++;
}
Return tmp;
}

From:
Http://www.pcjx.com/Dotnet/C/208333.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.