Simulate 5 unique numbers between 1-50 How do I simulate 5 random numbers, between 1 and 50, which are all unique?
I.e. no duplicates
 A: [This sounds like a self-study problem, so I'll answer with general strategy rather than a specific solution to the particular instance.]
The issue is essentially sampling without replacement rather than with replacement.
There are a variety of ways of implementing sampling without replacement. 
Indeed many packages offer explicit functions for this (such as R's sample which does sampling both with and without replacement).
Here's one way that can usually be implemented fairly easily but on some specific platforms may be easier using other approaches.
Here I discuss the problem of sampling values 1,2,...,n with replacement (I think you originally wanted to run it from 0 to n but that's a simple matter to deal with).
I will assume you have access to integer vectors (one dimensional arrays) and a source of discrete uniforms on $1,2,...,k$ for any specified $k$ and that arrays (just as in mathematics) are indexed from 1. You also need a single integer to index the current "end" of the vector -- effectively a pointer but you don't need a language with pointers for this. [The adjustments for other cases than this are not complicated.]
$\:$Step 0: fill a vector with the $n$ values you want to sample from
Then repeat steps 1-3 until you have all the values you need (or the values are exhausted)


*

*select an element at random from the $n$ available [generate a random value on $1..n$] and call it $i$, and output the value at that position (or store it or whatever). 

*copy the $n$th element of the vector over the $i$th.

*reduce $n$ by 1
This is essentially Durstenfeld's improvement of the Fisher-Yates algorithm.
This algorithm is fine for very small problems. If you want a general high quality algorithm (for production use say where you don't know bounds on the values it might be used for), however, you need to be aware that this produces permutations and while $P(50,5)$ (or $^{50}P_5$) is only about 254 million, with larger numbers there are more permutations than unique values that can be output by many RNGs. In that case not all permutations can be generated, and for some uses that can be a problem. Your problem is only interested in the combinations, and that makes it less of an issue, since the resulting inequality in the probabilities of the resulting combinations (only about 2.1 million of those in your case) may not be much of an issue.
See the discussion here of that issue. There are some additional issues (which shouldn't affect you enough to worry about) that are also raised there.
If you want a generally suitable algorithm you may want to consider other strategies; there are a variety of shuffling algorithms that might be used.
Another alternative approach for your simple problem is simple rejection sampling. If you get a number already generated you can try again. 
In fact on your problem, there's about an 80% chance you'll generate all 5 numbers without any clashes, so for that problem at least, you could generate all 5 with replacement, and then if there are any repeats, you could sample again. However if you're sampling without replacement from nearly all of a large population, either approach may be impractical. [On the other hand, if you need more than half the values, you can instead randomly choose what values to leave out of your sample; that way you never have to select more than half.]
--
An interesting thought occurs as I discuss that alternative -- one might set up a vector as with the original algorithm I mentioned, and then sample with replacement until you see a duplicate (storing the values generated thus far), then outputting all the values you generated to that point. Then shrink the list you generate from by all the values generated so far (following the above trick of copying the end value over and reducing the list length), and continue.
One quick but space-hungry way to see if you have repeated a value would be to have a vector of booleans of the same length as you're sampling from, (called "used" say) -- intialized all false, and flipped to true as you generate that value (or vice versa, call it "free" and initialize all true). As you generate new values you check if you can use it. When you shorten the original list you could apply the same update to this vector (resulting in copying false booleans over the true ones, so you could as easily just set the values in those positions -- always true -- to false)
(Another way to proceed with this would be to use a heap for the values you generate until you hit a duplicate)
So for example, if you wanted to sample 500 values out of a thousand without replacement, you'd typically sample (i.e. the median in this birthday problem) about 37 or 38 values before hitting a duplicate. You'd then shrink your set so the generated values are all eliminated and try again, so you're only performing the shrinking operation 12 or 13 times rather than 500 times.
However, it might be more efficient to apply sampling not simply to the first rejection but to do rejection sampling until you hit some larger proportion of rejections and then do the reduction (though this complicates the algorithm a bit more).
A: In addition to the existing answer, a slight variation that would not need you to keep a vector of lenght n in memory.


*

*Select a number between 1 and n, reduce n by 1

*Repeat step 1 untill you have k numbers

*Interpret your output


Here the most complicated part is 3, so I will give an example:
n: 50
k : 3
Result at step 3: [10, 20,15]

Interpretation: 


*

*10th available number in 1 to 50 is 10 

*20th available number in 1 to 50 excluding 10 is 21 

*15th available number in 1 to 50 excluding 10 and 21 is 16

