title: How to do a number of random trials efficiently?
body: <p>Let say an event have the <em>probability</em> <code>P</code> to succeed. <code>(0 &lt; P &lt; 1 )</code>
  and I have to make <code>N</code> tests to see if this happens and I want the total number of successes:</p>
  
  <p>I could go </p>
  
  <pre><code>int countSuccesses = 0;
  while(N-- &gt; 0)
  {
     if(Random.NextDouble()&lt;P) countSuccesses++; // NextDouble is from 0.0 to 1.0
  }
  </code></pre>
  
  <p>But is there not a more efficient way to do this? I want to have a single formula so I just can use ONE draw <del>random number</del> to determine the total number of successes. (<strong>EDIT</strong> The idea of using only one draw was to get below O(n))</p>
  
  <p>I want to be able call a method</p>
  
  <pre><code>GetSuccesses( n, P)
  </code></pre>
  
  <p>and it to be O(1)</p>
  
  <p><strong>UPDATE</strong><br>
  I will try to go with the
  <code>MathNet.Numerics.Distributions.Binomial.Sample(P, n)</code>
  even if it might be using more then only one random number I'll guess it will be faster than <code>O(n)</code> even if its not <code>O(1)</code>.  I'll benchmark that. Big thanks to David and Rici.</p>
  
  <p><strong>UPDATE</strong><br>
  The binomial sample above was O(n) so it did not help me. But thanks to a comment done by Fred I just switched to<br>
  <code>MathNet.Numerics.Distributions.Normal.Sample(mean, stddev)</code>
  where<br>
  <code>mean = n * P</code><br>
  <code>stddev = Math.Sqrt(n * P * (1 - P));</code>
  and now it is <code>O(1)</code> !</p>