Use C to generate code that does not repeat random Numbers

  • 2020-05-09 19:06:07
  • OfStack

For random Numbers, we all know that the computer can not produce completely random Numbers, so called random number generators are through 1 definite algorithm on the pre-selected random seeds to do complex operations, with the results produced to approximate the simulation of completely random Numbers, this kind of random Numbers are called pseudo-random Numbers. Pseudorandom Numbers are selected from a finite set of Numbers with the same probability. The chosen Numbers are not completely random, but they are sufficiently so from a practical point of view. The selection of pseudorandom Numbers starts from random seeds, so the selection of random seeds is very important in order to ensure that the pseudorandom Numbers obtained every time are sufficiently "random". If the random seed 1 sample, then the random number generated by the same random number generator will also be 1 sample. As a rule, we use parameters related to system time as the random seed, which is also the default method for random number generators in net Framework.

We can initialize a random number generator in two ways:

The first method does not specify a random seed, and the system automatically selects the current time as a random seed:


Random ro = new Random();


The second method can specify 1 int type parameter as the random seed:


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


This will ensure that 99% is not one.
After that, we can use this Random class object to generate random Numbers using the Random.Next () method. This method is quite flexible, you can even specify the upper and lower limits of the random Numbers generated.

The usage without specifying the upper and lower limits is as follows:


  int iResult; 
  iResult=ro.Next();


The following code specifies a random number that returns less than 100:


  int iResult; 
  int iUp=100; 
  iResult=ro.Next(iUp);


The following code specifies that the return value must be in the range of 50-100:


  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 floating point number between 0.0 and 1.0:


  double dResult; 
  dResult=ro.NextDouble();


However, when the question number is generated by Random class, there will be repetition, especially in the case of a small number of questions, it is difficult to generate a question that is not repeated. This paper refers to some methods on the Internet, including two types. The second class USES data structures and algorithms. The following sections focus on a few methods for class 2.

      method 1: the idea is to use an array to hold the index number, randomly generated an array position first, and then taking out the location of the index number, and make a copy of the final index number 1 to the current array location, and then make the maximum number of random minus 1, specific such as: first, put the 100 number in one array, each take a random location (first is 1-100, second is 1-99,...). , replace the number in this position with the last number.

int[] index = new int[15];
  for (int i = 0; i < 15; i++)
    index = i;
  Random r = new Random();
  // Used to hold randomly generated non-duplicates 10 The number of 
  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);
    // Pick it at random 1 Number, save to the result array 
    result[j] = index[id];
    // The last 1 Number of copies to the current position 
    index[id] = index[site - 1];
    // The lower limit of position is reduced 1
    site--;
  }

Method 2: use Hashtable.

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, which is used to detect whether the generated random Numbers have duplicates, and if there are duplicates between the extracted Numbers and the obtained Numbers, it will get them randomly 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 access 
    arrNum=getNum(arrNum,tmp,minValue,maxValue,ra); // Extract the value and assign it 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 the loop to determine if there is duplication 
      {
        tmp=ra.Next(minValue,maxValue); // Get it randomly again. 
        getNum(arrNum,tmp,minValue,maxValue,ra);// recursive : If there is a repeat between the number and the number obtained, it is retrieved randomly. 
      }
    n++;
    }
    return tmp;
  }


- attached:
In.net Framework, a class System.Random is provided specifically for generating random Numbers.
The computer itself can not produce completely random Numbers, so called random number generators are through 1 definite algorithm to the pre-selected random seeds to do complex operations, with the results produced to approximate the simulation of completely random Numbers, this kind of random number is called pseudorandom number.
Pseudorandom Numbers are selected from a finite set of Numbers with the same probability.

The chosen Numbers are not completely random, but they are sufficiently so from a practical point of view. The selection of pseudorandom Numbers starts from random seeds, so the selection of random seeds is very important in order to ensure that the pseudorandom Numbers obtained every time are sufficiently "random". If the random seed 1 sample, then the random number generated by the same random number generator will also be 1 sample. As such, we use the parameters related to system time as the random seed, which is also the default method for random number generators in net Framework.


Related articles: