0. N := 0; 1. NewValue := RandomRange(0, Max+1) 2. Loop through each value in the list: is it the same as NewValue? 3. If so, then repeat 1..2 until you come up with a value that is not in the array yet 4. Add NewValue to array 5. Inc(N) 6. If (N = NRequired) then STOP
Thaddy, wow, that's fantastic!!! I knew there should have been a more efficient way to!
When you google you will often find the same sources. Note that I found a direct link to the C++ sourcecode for the class that I translated. But indeed, after a few clicks on molly's links you finally arrive at the same spot.And that reaction by Eugene was about the sattoro code, which is different from Molly's links.
 Ah I see, Molly links to Fisher Yates. Sattoro is not Fisher Yates...It is an improvement on Fisher Yates (just one cycle) . Same answer:independent.
No, that answer on stackoverflow is a naive implementation of the same (Sattoro) optimization if I read it correctly.He actually initializes a whole array for the range needed + 1. (1001)Thus it wastes space for one element.Sattoro uses space exactly as is needed. (1000)So, yes, he came up with a solution that in effect is like Sattoro's, but his implementation is not optimal (one may say wrong, but that doesn't do him justice).My implementation is EXACT Sattoro and allocates space for the EXACT number of elements.