Binomial random number generation
<p>I'm struggling to get a random number generator in C++.</p>

<ul>
<li>The generator should only return integers between <code>0</code> and <code>n</code>.</li>
<li>The probability of returning low numbers should be higher than the probability of high numbers.</li>
</ul>

<p>Example distribution:</p>

<pre><code>1:  ************************
2:  ******************
3:  **************
4:  ************
5:  ********
6:  *****
7:  ****
8:  ***
9:  **
10: *
</code></pre>

<p>The distribution type doesn't matter in my case. What I have tried is to use a binomial distribution with the values <code>[0..2*n]</code>. Afterwards I convert the resulting random numbers into <code>[0..n]</code> to get the peak at zero.</p>

<pre><code>size_t n = 20;
std::default_random_engine generator;
std::binomial_distribution&lt;int&gt; distribution(n*2, 0.5f);
int number = fabs(distribution(generator)-n);
</code></pre>

<p>Resulting numbers:</p>

<pre><code>0: *************************
1: ***********************************************
2: *****************************************
3: ********************************
4: **********************
5: **************
6: ********
7: ****
8: **
9:
10-20: none. The numbers are very rare.
</code></pre>

<p>My question: How do you correctly implement such an algorithm? How can I increase the probability of higher values so that the distribution stays the same regardless of the used <code>n</code>?</p>