This project is a console app written in C#
Generate a list of 10,000 numbers in random order each time it is run.
Each number in the list must be unique and be between 1 and 10,000 (inclusive)
Just clone / download the repo, open
csharp-solution\NumberGenConsole.slnin VS.NET 2017 and run it.
Running in release mode is obviously advised.
4 different methods were explored
Run a benchmark: every method / algorithm is executed 100 times, then average execution time is outputted.
Execute a simple test to make sure numbers array is as expected.
The 4 methods are as follow:
We use the Random class to generate random numbers, which are added to a HashSet
We declare an int array and initialize it so that we have an ordered array, ie.
NbrList = 1 NbrList = 2 NbrList = 3 NbrList = 100 etc...
Then we use
Randomto randomize numbers array (not the fastest option, I included it just for fun)
Exactly the same as method #2, except we use a custom extension / helper method to shuffle our int array using Fisher-Yates algorithm
Fisher-Yates algorithmtime complexity is O(n), which makes it far more efficient than the "classic" naive implementation
We use the Random class to generate random numbers, which are added to a Dictionary
My personal pick is the third one.