Randomly generate K random numbers ranging from 1 to n. How many different random numbers are there?

Source: Internet
Author: User

How many different random numbers can you obtain if you want to randomly generate K random numbers in the range of 1-n? At the beginning, I thought that K random numbers could be obtained when K is less than or equal to N, but obviously this idea is wrong. An experiment was conducted to randomly generate 500 numbers in 1-394, of which there were only 1000 different numbers, and 639 numbers were randomly generated, of which were different.

Next is a very boring mathematical derivation. If you just want to look at the final formula, you can look at the second to last line. If you want to see the derivation process, take a look. The following describes the non-homogeneous recurrence of linear constants in probability and composite mathematics.

Now we want to calculate how many random numbers can be obtained by randomly generating K random numbers in the range of 1-N.

Set the K numbers we obtain at random K to x1, x2,... XK.

Set E [I] to the expected number of non-repeated numbers obtained at random I.

Let p [I] be the probability of repeating one of the XI and x1, x2,..., X [I-1] Random obtained at the I-th.

Set Q [I] to the probability that the XI and x1, x2,..., X [I-1] in the I-th random will not be repeated.

Then obviously P [I] = E [I-1]/n, qi = (n-e [I-1])/n.

Set Yi as the indicator variable. Yi = 1 indicates XI and x1, x2 ,......, in X [I-1], no one is repeated. Yi = 0 represents XI and x1, x2 ,......, one of the X [I-1] duplicates.

Therefore, E [I] can be used to obtain e [I] = sigma (1 * Q [J]) + sigma (0 * P [J]). = sigma (1 * Q [J]) = sigma (n-e [J])/n) {0 <= j <= I-1 }.

Now we can get E [I] = I-(1/n) * sigma (E [J]) {0 <= j <= I-1 }. Now we get a recursive relationship, and we know that E [0] = 0, E [1] = 1. We can use this recursive relationship to find e [I].

Of course, we don't just stop here. We will continue to study this recursive relationship and find a general formula.

E [ 1 ] = 1  E [  2 ] = 2 - 1 /N * E [ 1  ] E [  3 ] = 3 - 1 /N * (E [ 1 ] + E [ 2  ]) E [ 4 ] = 4 - 1 /N * (E [ 1 ] + E [ 2 ] + E [ 3  ])... E [k] = K- 1 /N * (E [ 1 ] + E [ 2 ] + E [ 3 ] + ...... + E [k- 1  ]) We will use the following formula to subtract the above formula to get: E [  2 ]-E [1 ] = 1 - 1 /N * E [ 1  ] E [  3 ]-E [ 2 ] = 1 - 1 /N * E [ 2  ] E [  4 ]-E [ 3 ] = 1 - 1 /N * E [3  ]... E [k] -E [k- 1 ] = 1 -( 1 /N) * E [k- 1 ]

Now set S [I] = E [1] + E [2] + ...... + E [I], we get the following recursive formula: s [k]-s [1]-s [k-1] = K-1-(1/n) * s [k-1].

Since s [1] = 1, we get: s [k]-(n-1)/n * s [k-1] = K. This is a non-homogeneous recurrence relation of linear constants. For this recurrence relation, the formula for finding a common term is detailed in the combination of mathematics.

By solving this recursive relationship, we obtain the general equation S [k] = (1-2 * n + N * n) * (n-1)/n) ^ (k-1) + (1-N) * n + N * K.

Bringing s [k-1] into E [k:E [k] = k-(1/n) * (1-2 * n + N * n) * (n-1)/n) ^ (K-2) + (1-N) * n + N * (k-1 )).

When k tends to be infinite, E [k] = k-(1/n) * (0 + (1-N) * n + N * (k-1) = n, it is consistent with our intuition.

 

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.