Creating a random std::vector with ones and zeros C++
<p>I want to create a vector of random 1's and 0's in a proportion set by me (in the program I called it dropout) The vector is the same size of a previously created vector CSUM.</p>

<p>in MATLAB it would be</p>

<pre><code>dropout=0.9;
n_elements=size(CSUM)
drpoutmask = (rand(n_elements) &gt; dropout); 
</code></pre>

<p>in C++ I have</p>

<pre><code>size_t elements = Csum.size();
std::vector&lt;float&gt; y(elements);
std::uniform_real_distribution&lt;float&gt; distribution(0.0f, 1.0f); 
std::mt19937 engine; // Mersenne twister MT19937
auto generator = std::bind(distribution, engine);
std::generate_n(y.begin(), elements, generator);
std::vector&lt;int&gt; dropoutmask(elements,0);
float dropout=0.9;

for(int i=0; i&lt;elements; i++)
  {
  if(y.at(i)&gt;dropout)
    {
    dropoutmask.at(i)=1;
    }
  }
}
</code></pre>

<p>which works but for huge vectors is very very slow, is there a faster way to do this? I am very new at C++.</p>

<p>Any help will be much appreciated</p>