<s>[INST] Trouble calculating probability Machine generates one random integer in range ${[0;40)}$ on every spin.
You should choose 5 numbers in that range.   
Then the machine will spit out 5 numbers (numbers are independent of each other).  
what is the probability that you will get exactly two numbers correct?

My logic:
You should get two of them right. chance of that is: $r = { \left( 1 \over 40 \right)^ 2 }$
You should get 3 of wrong. Chance of that is: $w = { \left( 39 \over 40 \right)^3 }$
As order doesn't matter answer should be: $$ans = { rw \over 2!3!}$$
Simulator tells me I'm wrong. Where is my logic flawed?
P.s. Machine can spit out duplicates
 [/INST] If the machine is choosing with replacement, but the person is choosing five distinct numbers, and if success means two distinct numbers the person chose are picked by the machine, then the problem is much more difficult:
Probability machine chooses five distinct numbers:
$$\dfrac{40!}{35!40^5}$$
Probability machine chooses four distinct numbers (choose the four distinct numbers the machine selected, choose the one number that is repeated twice, permute the multiset to find all possible orders these numbers could be chosen, divide by the total number of ways to choose five numbers):
$$\dfrac{\dbinom{40}{4}\dbinom{4}{1}\dfrac{5!}{1!1!1!2!}}{40^5}$$
Probability machine chooses three distinct numbers (choose the three distinct numbers the machine selected, either one number is repeated three times or two numbers are repeated twice each):
$$\dfrac{\dbinom{40}{3}\dbinom{3}{1}\left(\dfrac{5!}{3!1!1!}+\dfrac{5!}{2!2!1!}\right)}{40^5}$$
Probability machine chooses two distinct numbers (choose the two distinct numbers the machine selected. You can have one number four times and the other once or one number three times and the other twice.):
$$\dfrac{\dbinom{40}{2}\dbinom{2}{1}\left(\dfrac{5!}{4!1!}+\dfrac{5!}{3!2!}\right)}{40^5}$$
So, the probability of you matching exactly two numbers:
$$\begin{align*} & \dfrac{40!}{35!40^5}\cdot \dfrac{\dbinom{5}{2}\dbinom{35}{3}}{\dbinom{40}{5}}\\ + & \dfrac{\dbinom{40}{4}\dbinom{4}{1}\dfrac{5!}{2!}}{40^5}\cdot \dfrac{\dbinom{4}{2}\dbinom{36}{3}}{\dbinom{40}{5}} \\ + & \dfrac{\dbinom{40}{3}\dbinom{3}{1}\left(\dfrac{5!}{3!}+\dfrac{5!}{(2!)^2}\right)}{40^5}\cdot \dfrac{\dbinom{3}{2}\dbinom{37}{3}}{\dbinom{40}{5}} \\ + & \dfrac{\dbinom{40}{2}\dbinom{2}{1}\left(\dfrac{5!}{4!}+\dbinom{5}{3}\right)}{40^5}\cdot \dfrac{\dbinom{2}{2}\dbinom{38}{3}}{\dbinom{40}{5}} \approx 0.09116015625\end{align*}$$
Note: It is possible for the machine to choose one distinct number, but there is a zero probability that you wind up with two matching numbers, so I ignored that case. To show that my numbers work out though, you can test that the following holds:
$$\dfrac{40!}{35!}+\dbinom{40}{4}\dbinom{4}{1}\dfrac{5!}{2!1!1!1!}+\dbinom{40}{3}\dbinom{3}{1}\left(\dfrac{5!}{3!1!1!}+\dfrac{5!}{2!2!1!}\right)+\dbinom{40}{2}\dbinom{2}{1}\left(\dfrac{5!}{4!1!}+\dfrac{5!}{3!2!}\right)+\dbinom{40}{1} = 40^5$$
(I verified this is true myself using Wolframalpha).
</s>