but, if you are not playing around and writing a sort yourself or similar classroom work, why not generate the values in sorted order and skip the sort? Even something simple like
That will not work. It cannot produce the same probability distribution as randomly generating and then sorting.
Consider a numerical example:
Imagine that your original numbers have to be between 0 and 999. This is easy to ensure if you generate them randomly and then sort. However, there is no conceivable probability distribution that could be applied ADDITIVELY and GUARANTEE that none of the final numbers exceeded 999, yet still allows a finite probability of a sequence like {0, 1, 998, 999}. To get this additively you would have to allow each addition to possibly reach +997, ... but if this took place every time then it would lead to numbers far in excess of 999.
#include <algorithm>
#include <ctime>
#include <random>
#include <vector>
// generate a sorted sequence of n random integers
// in the range [min,max] invariant: min<max
std::vector<int> generate_rn( std::size_t n, int min, int max )
{
std::vector<int> result( n ) ;
// http://en.cppreference.com/w/cpp/numeric/randomstatic std::mt19937 rng( std::time(nullptr) ) ;
std::uniform_int_distribution<int> distr( min, max ) ;
std::generate_n( result.begin(), result.size(), [&](){ return distr(rng); } ) ;
std::sort( result.begin(), result.end() );
return result ;
}
much better.
Yes, I knew my quick crack at it was flawed, I was having trouble coming up with a proper implementation but it got the idea across (that the sort can be avoided).
Yes, I understand that fully along with the many issues with rand() & %.
There is no requirement from the OP, so any stream of sorted values will do, regardless of distribution or range of values or bias concerns. So assuming that any list of numbers that is sorted will do(maybe he just wants to test a binary search routine), you can generate directly. I said that above... it depends on what is needed.
Which brings us to JL's code.
You could fill in the bucket vector of the bucket sort with a uniform distribution of 1000 and create the target from that, if approximately 1M numbers instead of exactly 1M will do. There again, it depends on the needs. This may even be preferable, as it exercises the code on even and odd sized input vectors, and so on.
In all cases it depends on what the need is whether the shortcut will work. If the actual requirement were exactly 1M, 1000 range, uniform, sorted, and having variation across program runs, there is no shortcut worth taking, agreed. Remove one or more of those requirements (excepting sort) and it becomes possible.
Sometimes, you just want some numbers, and that is all you need.