Three methods for generating random numbers without duplicates

Source: Internet
Author: User
The following describes three methods to generate random numbers that are not repeated:
1. implemented through the while LOOP
A random number is generated continuously through the while loop until a non-repeating number is generated. This method is easy to think of, but the efficiency is also relatively low. The instance code is as follows:
Static void Main (string [] args)
{
Int [] result = new int [10];
Int tmp =-1;
Random random = new Random ();
Bool repeat = false;
For (int I = 0; I <10; I ++)
{
Repeat = true;
While (repeat)
{
Repeat = false;

Tmp = random. Next (1, 11 );
For (int j = 0; j <I; j ++)
{
If (tmp = result [j])
{
Repeat = true;
Break;
}
}
}
Result [I] = tmp;

}

For (int I = 0; I <10; I ++)
Console. WriteLine (result [I]. ToString ());
}
2. implemented through the for Loop
Method 1 uses multiple nested loops, which is very inefficient. Therefore, I apply certain skills to reduce nested loops to improve program efficiency. The main idea is to reduce the cyclic variable by 1 if the recurrence is detected, so as to re-execute a loop and re-generate a random number until a non-repeating random number is generated. The instance code is as follows:
Static void Main (string [] args)
{
Int [] result = new int [10];
Int tmp =-1;
Random random = new Random ();
Bool repeat = false;
For (int I = 0; I <10; I ++)
{
Repeat = false;

Tmp = random. Next (1, 11 );
For (int j = 0; j <I; j ++)
{
If (tmp = result [j])
{
Repeat = true;
Break;
}
}
If (! Repeat)
{
Result [I] = tmp;
}
Else
{
I = I-1; // The cyclic variable-1
}

}

For (int I = 0; I <10; I ++)
Console. WriteLine (result [I]. ToString ());
}
This method reduces the number of nested loops and improves the efficiency!
3. Implement random sorting
This method completely subverts the basic ideas of methods 1 and 2. First, initialize an array containing numbers 1-10, and then take a random position for each loop, swap the element at this position with the element at the last position! The instance code is as follows:
Static void Main (string [] args)
{
Int [] result = new int [10];
For (int I = 0; I <10; I ++)
Result [I] = I + 1;
For (int j = 9; j> 0; j --)
{
Random r = new Random ();
Int index = r. Next (0, j );
Int temp = result [index];
Result [index] = result [j];
Result [j] = temp;
}

For (int I = 0; I <10; I ++)
Console. WriteLine (result [I]. ToString ());

}
This method eliminates loop nesting and further improves the efficiency, but it also has certain limitations. If we want to generate a random number between 5 and 10, this method of disordered order cannot be used!

Conclusion: method 1 is inefficient and is generally not recommended!
Method 2 is more common, the efficiency is higher than method 1, but the efficiency is lower than method 3
Method 3, although highly efficient, can only be applied to specific situations!

Please kindly advise!

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.