C # generates non-repeating random numbers (RPM)

Source: Internet
Author: User
Tags random seed

When we do an exam system that automatically generates test papers, we often need to randomly generate a random set of random numbers, and in the. NET Framework, we provide a class system.random that is designed to generate random numbers.
For random numbers, we all know that the computer can not produce a completely random number, so-called random number generator is a certain algorithm on the pre-selected random seed to do a complex operation, with the resulting results to approximate the simulation of a complete random number, this random number is called pseudo-random number. Pseudo-random numbers are selected from a finite set of numbers in the same probability. The selected number is not completely random, but from a practical point of view, its randomness is sufficient. The selection of pseudo-random numbers starts with random seeds, so the selection of random seeds is very important in order to ensure that the pseudo-random numbers are sufficiently "random" for each given. If the random seed is the same, then the random number generated by the same random number generator will be the same. In general, we use parameters associated with the system time as random seeds, which is the default method used by the random number generators in the. 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 an int parameter as a 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 ensures that 99% is not the same. After the
, we can use the object of the random class to generate a random number, which is the Random.next () method. This method is quite flexible and you can even specify the upper and lower bounds of the resulting random number.  The
do not specify the upper and lower bounds using the following:
int iresult;
Iresult=ro. Next ();  
The following code specifies that a random number less than 100 is returned:
int iresult;
int iup=100;
Iresult=ro. Next (IUP);  
The following code specifies that the return value must be within 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 provides the random.nextdouble () method to produce a random double-precision floating-point number that ranges between 0.0 and 1.0:
Double dresult;
Dresult=ro. Nextdouble ();
but using the random class to generate the question number, there will be duplicates, especially in a small number of topics to generate a non-repeating topic is difficult, referring to some online methods, including two categories, one is to start with random seed, so that each random seed different, to ensure that the non-repetition The second type is the use of some data structures and algorithms. Here are a few ways to introduce the second class.

    Method 1: The idea is to use an array to hold the index number, generate an array position randomly, then take the index number of the position, and copy the last index number to the current array position, and then subtract the upper limit of the random number by one. For example: First put these 100 numbers in an array, each random take a position (the first time is 1-100, the second is 1-99, ...) ) to replace the number of the position with the last number.

int[] index = new INT[15];
for (int i = 0; i < i++)
index = i;
Random r = new Random ();
Used to save randomly generated 10 number of non-repeating
Int[] result = new INT[10];
int site = 15;//Set lower limit
int id;
for (int j = 0; J <; J + +)
{
id = r.next (1, site-1);
Take a number out of a random position and save it to the result array
RESULT[J] = Index[id];
The last number is copied to the current location
Index[id] = index[site-1];
The lower limit of the position is reduced 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: Recursive, it is used to detect whether the generated random number is duplicated, if the number taken out and the number obtained has been repeated randomly retrieved.

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 Fetch number
Arrnum=getnum (Arrnum,tmp,minvalue,maxvalue,ra); The value of the fetch 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 if there are duplicates
{
Tmp=ra. Next (Minvalue,maxvalue); Re-randomly acquired.
Getnum (Arrnum,tmp,minvalue,maxvalue,ra);//recursion: If the number that is taken out and the number obtained is duplicated, it is retrieved randomly.
}
n++;
}
return TMP;
}

C # generates non-repeating random numbers (RPM)

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.