Example #1
0
        /// <summary>
        /// Returns a fully uniformly distributed vector (corresponds to a
        /// uniformly distributed point on the surface of the unit sphere).
        /// Note however, that the returned vector will never be equal to
        /// [0, 0, -1].
        /// </summary>
        public static V3d UniformV3dFullDirection(this IRandomUniform rnd)
        {
            double phi = rnd.UniformDoubleFull() * Constant.PiTimesTwo;
            double z   = 1.0 - rnd.UniformDoubleFull() * 2.0;
            double s   = System.Math.Sqrt(1.0 - z * z);

            return(new V3d(System.Math.Cos(phi) * s, System.Math.Sin(phi) * s, z));
        }
Example #2
0
 /// <summary>
 /// Returns a uniformly distributed long in the interval [0, size-1].
 /// NOTE: If count has more than about 48 bits, aliasing leads to
 /// noticeable (greater 5%) shifts in the probabilities (i.e. one
 /// long has a probability of x and the other a probability of
 /// x * (2^(52-b)-1)/(2^(52-b)), where b is log(size)/log(2)).
 /// </summary>
 public static long UniformLong(this IRandomUniform rnd, long size)
 {
     if (rnd.GeneratesFullDoubles || size < 16777216)
     {
         return((long)(rnd.UniformDouble() * size));
     }
     else
     {
         return((long)(rnd.UniformDoubleFull() * size));
     }
 }
Example #3
0
 /// <summary>
 /// Returns a uniformly distributed int in the interval [0, count-1].
 /// In order to avoid excessive aliasing, two random numbers are used
 /// when count is greater or equal 2^24 and the random generator
 /// delivers 32 random bits or less. The method thus works fairly
 /// decently for all integers.
 /// </summary>
 public static int UniformInt(this IRandomUniform rnd, int size)
 {
     if (rnd.GeneratesFullDoubles || size < 16777216)
     {
         return((int)(rnd.UniformDouble() * size));
     }
     else
     {
         return((int)(rnd.UniformDoubleFull() * size));
     }
 }
Example #4
0
        /// <summary>
        /// Fills the specified array with fully random doubles (53 random
        /// bits) in the half-open interval [0.0, 1.0).
        /// </summary>
        public static void FillUniformFull(
            this IRandomUniform rnd, double[] array)
        {
            long count = array.LongLength;

            if (rnd.GeneratesFullDoubles)
            {
                for (long i = 0; i < count; i++)
                {
                    array[i] = rnd.UniformDoubleFull();
                }
            }
            else
            {
                for (long i = 0; i < count; i++)
                {
                    array[i] = rnd.UniformDouble();
                }
            }
        }
Example #5
0
 public static V3d UniformV3dFull(this IRandomUniform rnd, Box3d box)
 {
     return(box.Lerp(rnd.UniformDoubleFull(),
                     rnd.UniformDoubleFull(),
                     rnd.UniformDoubleFull()));
 }
Example #6
0
 public static V3d UniformV3dFull(this IRandomUniform rnd)
 {
     return(new V3d(rnd.UniformDoubleFull(),
                    rnd.UniformDoubleFull(),
                    rnd.UniformDoubleFull()));
 }
Example #7
0
 public static V2d UniformV2dFull(this IRandomUniform rnd, Box2d box)
 {
     return(new V2d(box.Min.X + rnd.UniformDoubleFull() * (box.Max.X - box.Min.X),
                    box.Min.Y + rnd.UniformDoubleFull() * (box.Max.Y - box.Min.Y)));
 }