The problem is that random-number generators don't know or care about history (at least, not in the sense that you do--software random number generators use recent results to generate future results). So, your choices are generally to...
- Live with the possibility of duplicates. Obviously, that doesn't help you.
- Review previous results and discard duplicates by repeating the draw until the value is unique. This has the potential to never end, if your random numbers get stuck in a cycle.
- Enumerate the possible values, shuffle them, and pick the values off the list as needed. Obviously, that's not sensible if you have an enormous set of possible values.
In your case, I'd pick the third.
- Create an array (
int[]
) big enough to hold one of every possible value (1-49).
- Use a
for
loop to initialize each array value.
- Use another
for
loop to swap each entry with a random index. If you swap a position with itself, that's fine.
- Use the first few (6) elements of the array.
You can combine the second and third steps, if you want, but splitting them seems more readable and less prone to error to me.