Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In general to generate random numbers from an arbitrary distribution you'd first generate uniform random numbers and then pass them to the inverse of the cumulative distribution function.</p> <p>Assume for example that you want random numbers with uniform distribution on the interval [-10.0, 10.0] and all you've got is random numbers from [0.0, 1.0]. Cumulative distribution function of the uniform distribution on [-10.0, 10.0] is:</p> <pre><code>cdf(x) = 0.05 * x + 0.5 for x in [-10.0, 10.0] </code></pre> <p>This expresses the probability that a random number generated is smaller than x. The inverse is</p> <pre><code>icdf(y) = 20.0 * y - 10.0 for y in [0.0, 1.0] </code></pre> <p>(You can obtain this easily on paper by switching the x and y axis).</p> <p>Hence to obtain random numbers uniformly distributed on [-10.0, 10.0] you can use the following code:</p> <pre><code>#include &lt;stdlib.h&gt; // Returns uniformly distributed random numbers from [0.0, 1.0]. double uniform0to1Random() { double r = random(); return r / ((double)RAND_MAX + 1); } // Returns uniformly distributed random numbers from [-10.0, 10.0]. double myRandom() { return 20.0 * uniform0to1Random() - 10.0; } </code></pre> <p>In fact, you don't need uniform0to1Random() since there are already a lot of good uniform random numbers generators from [0.0, 1.0] (e.g. in the boost library).</p> <p>You can use the method to generate random numbers with nearly any probability distribution you want by sampling the inverse cumulative distribution as shown above.</p> <p>See <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling" rel="nofollow">http://en.wikipedia.org/wiki/Inverse_transform_sampling</a> for more details.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload