예제 #1
0
        static T Random <T>(IEnumerable <T> ts, MersenneTwister r, bool throws)
        {
            var xs = ts as ICollection <T>;

            xs = xs ?? ts.ToList();
            if (xs.Count == 0)
            {
                if (throws)
                {
                    throw new ArgumentException("Collection must not be empty.", "ts");
                }
                else
                {
                    return(default(T));
                }
            }
            else
            {
                return(xs.ElementAt(r.Next(xs.Count)));
            }
        }
예제 #2
0
 // Sampled a N-sample probability density function in the range [-1024..1024]
 // 1 sample produces a rectangular probability
 // 2 samples produces a triangular probability
 // ...
 // N samples approximates a true Gaussian
 public static WDist FromPDF(MersenneTwister r, int samples)
 {
     return(new WDist(Exts.MakeArray(samples, _ => r.Next(-1024, 1024))
                      .Sum() / samples));
 }