I need 100 random numbers from the range 1 to 1,000,000. The numbers must be unique, no duplicates.
It’s similar to this question, but my range is too large to create an array from.
I need to generate these 100 random numbers many, many times, so generation needs to be as fast as possible, preferably O(1). What’s the fastest way to do it?
I would use a HashSet and a Mersenne Twister.
code:
The output was:
320000 : nano
100
Fast enough? and yes, this is O(1) as you always do 100 numbers.
Beware of the Birthday paradox, here you select 100 out of 1000000, thats ok but if you boost the 100 up, it will take longer and longer.