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
 A: Let's say, you have chosen 5 numbers. Then the odds of the machine generating one of your chosen numbers is $\frac{5}{40}$. This happens twice and it doesn't happen thrice. So this should be $\left(\frac{5}{40}\right)^2\times\left(\frac{35}{40}\right)^3$. Now this could have happened in $\frac{5!}{2! \times 3!}$ (or $5 \choose 2$). 
So, is it $\left(\left(\frac{5}{40}\right)^2 \times \left(\frac{35}{40}\right)^3 \times {5 \choose 2} \right)$?
However, the assumption is that you choose distinct numbers in that range. In case, your strategy is to maximize the "correct" score. If you are picking at random without a strategy, then lulu's answer is the one you are looking for.  
A: This is a simple binomial problem.
Note:  I am assuming that both you and the machine choose with replacement.  That is, either (or both) of you might have duplicates.  
For a single choice you make, the probability that the machine also makes it (as one of the $5$ it chooses) is $\psi = 1 - \left( \frac {39}{40}\right)^5$.
As your choices are independent, the answer is then $$\binom 52\times \psi^2\times (1-\psi)^3\approx .0967$$
A: Here is the argument if neither you nor the machine can repeat.
Since all the numbers are equally likely let's assume you chose $\{1,2,3,4,5\}$. 
There are $T = \binom{40}{5}$ total ways the machine can choose its five numbers. Let's count how that choice might overlap with yours.
There are
$$
\binom{35}{5} \text{ choices with } 0 \text{ overlap}
$$
$$
\binom{5}{1}\binom{35}{4} \text{ choices with } 1 \text{ overlap}
$$
$$
\binom{5}{2}\binom{35}{3} \text{ choices with } 2 \text{ overlap}
$$
$$
\binom{5}{3}\binom{35}{2} \text{ choices with } 3 \text{ overlap}
$$
$$
\binom{5}{4}\binom{35}{1} \text{ choices with } 4 \text{ overlap}
$$
$$
1 \text{ choice with } 5 \text{ overlap}
$$
To find the probability of exactly $2$ matches, compute that number as above and divide by $T$, To find the probability of at least $2$ matches, add the probabilites of $0$ and $1$ match and subtract from $1$.
A: 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).
A: There are a few different errors in your calculation.  


*

*If the numbers you choose are $\ n_1, n_2, \dots, n_5\ $ (I'm presuming these have to be all different), and the numbers generated by the machine are $\ m_1, m_2, \dots, m_5\ $, then $\ \left(\frac{1}{40}\right)^2\ $ is merely the probability that any specified ordered pair of numbers generated by the machine are equal to some given pair of your chosen numbers (e.g. $\ m_1=n_1\ $ and $\ m_2=n_2\ $, for instance).  To allow for the fact that the order doesn't matter, you need to multiply, not divide, by $\ 2!\ $—that is, $$\ \mathrm{Prob}\left(\left\{m_1=n_1\, \&\, m_2=n_2\right\}\lor\left\{m_2=n_1\, \&\, m_2=n_1\right\}\right)=2\left(\frac{1}{40}\right)^2\ .$$  But you still haven't counted the cases where some other pair of the numbers chosen by the machine ($\ m_4\ $ and $\ m_5\ $, for example) are equal to your $\ n_1\ $ and $\ n_2\ $, let alone any other pair of your chosen numbers.

*As clarified in one of your comments, it is possible for more than $\ 2\ $ of the numbers generated by the machine to match some of those in your hand even when you have only exactly two right. If you choose numbers $\ 1,2,3,4,5\ $, for instance, and the machine generates the numbers $\ 6,7,1,8,2,1\ $,  then exactly $\ 2\ $ of your numbers—namely, $\ 1\ $ and $\ 2\ $—are among those generated by the machine, but three of those generated by the machine—namely the $\ 2\ $ and the two $\ 1$s—are among those you chose.  Also, once the machine has generated two of the numbers you have chosen, it cannot generate any of the remaining $\ 3\ $ you have chosen without increasing the number you've got correct to more than $\ 2\ $.  Thus, for both of these reasons, $\ \left(\frac{39}{40}\right)^3\ $ isn't the probability of your getting $\ 3\ $ wrong.


The easiest way I can see of calculating the probability you're looking for is to treat the accumulating number of matches between the numbers generated by the machine and those you have chosen as an inhomogeneous Markov chain.  Let $\ C\ $  be the set of numbers you have chosen (which I assume to be all distinct), $\ m_1, m_2, \dots, m_5\ $ the numbers generated by the machine, and $\ X_n = \left\vert \left\{m_i\right\}_{i=1}^n\cap C\right\vert, X_0=0\ $. Then $\ \left\{X_n\right\}_{i=1}^5\ $ is a Markov chain whose states and transitions are represented in the diagram below:
The numbers on the links are the transition probabilities, while the numbers inside the circles are the probabilities of the states being reached.  If my arithmetic is correct, the probability that exactly two of your chosen numbers appear amongst those generated by the machine is $\ \frac{2,337}{256,000}\approx 0.091\ $.
Update: It turns out there was an arithmetical error in my original calculations, which I have now corrected.  As a check on the result, here's a calculation of the number of sequences of $\ 5\ $ integers in the range $\ 0\ \mathrm{to}\ 39\ $, in which exactly two out of a given set, $\ S\ $, of $\ 5\ $ distinct such numbers occur.
There are $\ {5\choose2}\ $ different pairs of members of $\ S\ $ that could be the ones that occur in the sequence.


*

*If these occur exactly once each, then there are $\ 5\ $ positions of the sequence in which the lower of the two could be placed, and for each of these, there are $\ 4\ $ positions where the higher could be placed.  For each of these arrangements the remaining $\ 3\ $ positions have to be each filled with a number not in $\ S\ $, which can be done in $\ 35^3\ $ ways. Thus, there are total of $\ 5\cdot\ 4\cdot 35^3\ $ sequences of this type.

*If one of these numbers is duplicated in the sequence, there are $\ {5\choose 3}\ $ combinations of positions in the sequence where they can be placed, and for each of these there are $\ 6\ $ possible ways in which the two numbers can fill them (the non-duplicated number can be either of the two, and it can be in any of the three positions).  The remaining two positions have to be filled by numbers not in $\ S\ $, which can be done in $\ 35^2\ $ ways. Thus there are a total of $\ {5\choose3}\cdot6\cdot35^2\ $ sequences of this type.

*If the two numbers together occur a total of $\ 4\ $ times in the sequence, then there are $\ {5\choose4}\ $ combinations of positions where they can be placed, and there are $\ 2^4-2=14\ $ ways in which the two numbers can fill them.  For each of these, the remaining position in the sequence can be filled in $\ 35\ $ ways by a number not in $\ S\ $.  Thus, there are a total of $\ {5\choose4}\cdot14\cdot35\ $ sequences of this type.

*If all five numbers in the sequence are one of the two, there are $\ 2^5-2=30\ $ ways in which they can be chosen to fill the $\ 5\ $ positions.


Thus the total number of sequences with the desired property is $$\ {5\choose2}\left(5\cdot\ 4\cdot 35^3+{5\choose3}\cdot6\cdot35^2+{5\choose4}\cdot14\cdot35+30\right)\\=9,334,800\ ,$$
and the probability of the machine's generating one of these sequences is $\ \frac{9,334,800}{40^5}=\frac{23,337}{256,000}\ $, as obtained by the Markov chain calculation.
