Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerate a random number in a Gaussian Range?
    primarykey
    data
    text
    <p>I want to use a random number generator that creates random numbers in a gaussian range where I can define the median by myself. I already asked a similar question here and now I'm using this code:</p> <pre><code>class RandomGaussian { private static Random random = new Random(); private static bool haveNextNextGaussian; private static double nextNextGaussian; public static double gaussianInRange(double from, double mean, double to) { if (!(from &lt; mean &amp;&amp; mean &lt; to)) throw new ArgumentOutOfRangeException(); int p = Convert.ToInt32(random.NextDouble() * 100); double retval; if (p &lt; (mean * Math.Abs(from - to))) { double interval1 = (NextGaussian() * (mean - from)); retval = from + (float)(interval1); } else { double interval2 = (NextGaussian() * (to - mean)); retval = mean + (float)(interval2); } while (retval &lt; from || retval &gt; to) { if (retval &lt; from) retval = (from - retval) + from; if (retval &gt; to) retval = to - (retval - to); } return retval; } private static double NextGaussian() { if (haveNextNextGaussian) { haveNextNextGaussian = false; return nextNextGaussian; } else { double v1, v2, s; do { v1 = 2 * random.NextDouble() - 1; v2 = 2 * random.NextDouble() - 1; s = v1 * v1 + v2 * v2; } while (s &gt;= 1 || s == 0); double multiplier = Math.Sqrt(-2 * Math.Log(s) / s); nextNextGaussian = v2 * multiplier; haveNextNextGaussian = true; return v1 * multiplier; } } } </code></pre> <p>Then to verify the results I plotted them with gaussianInRange(0, 0.5, 1) for n=100000000 <img src="https://i.stack.imgur.com/1G8xa.jpg" alt="enter image description here"></p> <p>As one can see the median is really at 0.5 but there isn't really a curve visible. So what I'm doing wrong?</p> <h2>EDIT</h2> <p>What i want is something like this where I can set the highest probability by myself by passing a value.</p> <p><img src="https://i.stack.imgur.com/I0kTP.gif" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    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.
 

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