forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNdArrayRandom.cs
More file actions
60 lines (55 loc) · 2.13 KB
/
NdArrayRandom.cs
File metadata and controls
60 lines (55 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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; }
/// <summary>
/// Return a sample (or samples) from the “standard normal” distribution.
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
public NDArray<double> randn(params int[] size)
{
return this.stardard_normal(size);
}
/// <summary>
/// Draw random samples from a normal (Gaussian) distribution.
/// </summary>
/// <param name="loc">Mean of the distribution</param>
/// <param name="scale">Standard deviation of the distribution</param>
/// <param name="size"></param>
/// <returns></returns>
public NDArray<double> normal(double loc, double scale, params int[] size)
{
if (size.Length == 0)
throw new Exception("d cannot be empty.");
NDArray<double> array = new NDArray<double>();
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;
}
/// <summary>
/// Draw samples from a standard Normal distribution (mean=0, stdev=1).
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
public NDArray<double> stardard_normal(params int[] size)
{
return this.normal(0, 1.0, size);
}
}
}