Exemplo n.º 1
0
        /// <summary>
        /// Sample from the distribution tail (defined as having x >= __R).
        /// </summary>
        /// <returns></returns>
        private double SampleTail()
        {
            double x, y;

            do
            {
                // Note. we use NextDoubleNonZero() because Log(0) returns NaN and will also tend to be a very slow execution path (when it occurs, which is rarely).
                x = -Math.Log(_rng.NextDoubleNonZero()) / __R;
                y = -Math.Log(_rng.NextDoubleNonZero());
            }while (y + y < x * x);
            return(__R + x);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Return a random double in the range [min,max]
        /// </summary>
        /// <param name="min">Inclusive minimum of range</param>
        /// <param name="max">Inclusive maximum of range</param>
        /// <returns></returns>
        public double Inclusive(double min, double max)
        {
            if (min >= max)
            {
                throw new ArgumentOutOfRangeException($"Min [{min}] must be < Max [{max}]!");
            }
            return(_fastRandom.NextDoubleNonZero() * (max + double.Epsilon - min) + min);


            //if (min >= max) throw new ArgumentException($"Min [{min}] must be < Max [{max}]!");
            //var random1 = _fastRandom.NextDouble();            // random1 is in range [0,1)
            //var random2 = _fastRandom.NextDouble() * -1.0 + 1.0; // random2 is in range (0,1]
            //// random1 + random2 is in range (0,2), divide by 2 (multiply by 0.5) for range (0,1)
            //return (random1 + random2) * (max - min) * (0.5) + min;
        }
Exemplo n.º 3
0
 public void NextDoubleNonZero10M()
 {
     for (int i = 0; i < __loops; i++)
     {
         _rng.NextDoubleNonZero();
     }
 }
Exemplo n.º 4
0
        public void NextDoubleNonZero()
        {
            int            sampleCount = 10000000;
            XorShiftRandom rng         = new XorShiftRandom();

            double[] sampleArr = new double[sampleCount];

            for (int i = 0; i < sampleCount; i++)
            {
                sampleArr[i] = rng.NextDoubleNonZero();
                if (0.0 == sampleArr[i])
                {
                    Assert.Fail();
                }
            }

            UniformDistributionTest(sampleArr, 0.0, 1.0);
        }