Get random numbers that are not duplicated (draw lots principle)

Source: Internet
Author: User

Statement: The following methods are based on the drawing principle, each extraction of the same probability, that is, dependent on the previous results, if there are errors are as follows not set up.

At ordinary times, when we get random numbers that are not duplicated, we generally need to have a single figure, or a set record, or a loop nested comparison, to ensure that no duplicate numbers are selected, I also thought about this problem when using sampling today. Make the following record, as follows:

1. We would like to randomly extract 10 distinct numbers from the 100 numbers of 1~100

2, this is equivalent to find a sequence of 1 and 0, the sequence length of 100, where 1 of the number of 10

3, this is equivalent to a bag containing 100 balls, of which Bai 90, black Ball 10

4, from the bag in order to take 1 balls, not put back, record its color, and the resulting sequence

5, we can calculate the nth ball to take black probability, is 1/10

6, do not put back the probability of sampling is such as possible, such as the 2nd ball black probability for (10/100) * (9/99) + (90/100) * (10/99) = 1/10

7, this can be adjusted to take the random number each time, compare the current ratio of the ball, determine the color of the ball taken

8, according to the position of the black ball, determine the specific value of random numbers

The code is as follows:

		Double white = 10.0;
		Double black = 10.0;
		Double rate = 0.0;
		Random Random = new Random (System.currenttimemillis ());
		Double tmp = 0.0;
		int count = 0;
		while (true) {
			if (white <= 0.0 | |-Black <= 0.0) {break
				;
			}
			Rate = Black/black + white;
			TMP = Random.nextdouble ();
			if (tmp <= rate) {
				black--;				System.out.print ("1");
				System.out.print (Count + "");
			} else{
				white--;				System.out.print ("0");
			}
			count++;
		}
		while (White > 0.0) {
			--white;			System.out.print ("0");
			count++;
		}
		while (Black > 0.0) {
			--black;
			System.out.print (Count + "");			System.out.print ("1");
			count++;
		}
	

The results are as follows:



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.