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: