Most efficient way to populate a vector of integers with no duplicates I am trying to populate a vector of integers with one billion random number.  The only constraint is that there can be no duplicates in the array.

#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;vector&gt;
#include &lt;ctime&gt;

using namespace std;


int main()
{
    srand(time(NULL));

    vector&lt;int&gt; container;

    vector&lt;int&gt;::iterator i1;


    for (int i=0;i&lt;1000000000;i++) //a billion numbers
    {
        int number = rand() ; 

        i1 = find (container.begin(),container.end() , number );

        if ( i1 != container.end() )
        {
            container.push_back(number);
        }

    }    

}


How can my solution be improved on? It can be anything, like time taken or space complexity.
Your solution as it stands currently is going to be incredibly slow - it's complexity is O(n^2), so you're looking at roughly 10^18 "operations" for a billion numbers. This is because you need to search through the vector as it grows to see if you have a duplicate, which will go through the vector (on average) (1 + 2 + ... + 999,999,999) / 2 times over the course of the algorithm. 

Edit: In fact, it's actually slightly worse than this, since as you generate more numbers, the probability that you generate something you've already "seen" increases.

I'd suggest the following: since an int on most platforms is still 4 bytes, this still has a maximum value of a bit over 2 billion. Hence create a vector with the first billion values in order, and use std::shuffle. This will give you a billion "random" values, with the constraint that nothing will be selected from the upper range [1e9, INT_MAX].

int main()
{
    constexpr int num_values = 1000000000;
    std::vector&lt;int&gt; rand_values(num_values);
    std::mt19937 eng{std::random_device{}()};

    for(int i = 0; i &lt; num_values; ++i) {
        rand_values[i] = i;
    }

    std::shuffle(rand_values.begin(), rand_values.end(), eng);
}