using NumSharp.Core.Extensions; using System; using System.Collections.Generic; using System.Text; namespace NumSharp.Core { public class NDArrayRandom { public static int Seed { get; set; } /// /// Return a sample (or samples) from the “standard normal” distribution. /// /// /// public NDArray randn(params int[] size) { return this.stardard_normal(size); } /// /// Draw random samples from a normal (Gaussian) distribution. /// /// Mean of the distribution /// Standard deviation of the distribution /// /// public NDArray normal(double loc, double scale, params int[] size) { if (size.Length == 0) throw new Exception("d cannot be empty."); NDArray array = new NDArray(); Random rand = new Random(); //reuse this if you are generating many array.Shape = new Shape(size); array.Data = new double[array.Shape.Size]; for (int i = 0; i < array.Shape.Size; i++) { double u1 = 1.0 - rand.NextDouble(); //uniform(0,1] random doubles double u2 = 1.0 - rand.NextDouble(); double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); //random normal(0,1) double randNormal = loc + scale * randStdNormal; //random normal(mean,stdDev^2) array.Data[i] = randNormal; } return array; } /// /// Draw samples from a standard Normal distribution (mean=0, stdev=1). /// /// /// public NDArray stardard_normal(params int[] size) { return this.normal(0, 1.0, size); } } }