-1

i have a method that generates random numbers but doesn't seem to be following a Gaussian distribution, to further complicate this i am returning values as a 'byte'. I really have no idea how this algorithm works, i tried researching on wikipedia but to little avail. Does anyone know why this doesn't seem to be working?

        private byte RndGaussian(byte mean, byte stdDev)
    {
        // From stackoverflow user 'yoyoyoyosef'.
        double u1 = rnd.NextDouble(); // These are uniform(0,1) random doubles
        double u2 = rnd.NextDouble();
        double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); // Random normal(0,1)
        byte randNormal = Convert.ToByte(mean + stdDev * randStdNormal); // Random normal(mean,stdDev^2)
        return randNormal;
    }
Dom
  • 1,232
  • 1
  • 10
  • 20
  • This might be helpful http://stackoverflow.com/questions/218060/random-gaussian-variables – Sergiy Lichenko Jul 12 '16 at 23:09
  • Also have a look at this library: http://numerics.mathdotnet.com/Probability.html (don't reinvent the wheel) – P. Kouvarakis Jul 12 '16 at 23:13
  • 1
    The function is returning a byte, but the normal distribution returns real numbers (double). If you increase stDev to a large value this will begin to look like a wide bell, but I suspect that this is not what you want. – JerryM Jul 12 '16 at 23:20
  • You say that you have no idea *why this algorithm works*, and that *it doesn't work*. I am confused as to what you are actually asking here. – Eric Lippert Jul 13 '16 at 05:02
  • What i meant to say was that i don't know why it is meant to work, but i have managed to fix the problem. – Dom Jul 14 '16 at 12:43

1 Answers1

0

I fixed this (though i don't know why it works) but simply dividing the value i was putting in the stddev by 3 so that the range of values i wanted was achieved. It works fine as a byte return type.

Dom
  • 1,232
  • 1
  • 10
  • 20