예제 #1
0
        public void Gaussian_ZeroRsq_Test()
        {
            var mem    = new MemoryStream();
            var writer = new BinaryWriter(mem);

            writer.Write(0.5);
            writer.Write(0.5);

            var rng    = new NotRandomFromBytes(mem.ToArray(), true);
            var actual = rng.NextGaussian(); // should throw because rsq values are zero
        }
예제 #2
0
        public void Gaussian_KnownValue_Test()
        {
            var mem    = new MemoryStream();
            var writer = new BinaryWriter(mem);

            var d1 = 0.25;
            var d2 = 0.75;

            writer.Write(d1);
            writer.Write(d2);

            var rng    = new NotRandomFromBytes(mem.ToArray(), true);
            var actual = rng.NextGaussian();

            var r1       = d1 * 2 - 1;
            var r2       = d2 * 2 - 1;
            var rsq      = r1 * r1 + r2 * r2;
            var expected = r1 * Math.Sqrt(-2.0 * Math.Log(rsq) / rsq);

            Assert.AreEqual(expected, actual);
        }