■ 가우스 정규 난수기를 사용하는 방법을 보여준다.
▶ GaussianRandom.cs
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
namespace TestProject; /// <summary> /// 가우스 난수기 /// </summary> public class GaussianRandom { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 난수기 /// </summary> private Random random; /// <summary> /// 이용 가능 여부 /// </summary> private bool available; /// <summary> /// 다음 가수스 값 /// </summary> private double nextGaussianValue; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - GaussianRandom() /// <summary> /// 생성자 /// </summary> public GaussianRandom() { this.random = new Random(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 난수 값 구하기 - GetRandomValue() /// <summary> /// 난수 값 구하기 /// </summary> /// <returns>난수 값</returns> public double GetRandomValue() { if(this.available) { this.available = false; return this.nextGaussianValue; } double u1 = this.random.NextDouble(); double u2 = this.random.NextDouble(); double temporaryValue1 = Math.Sqrt(-2.0 * Math.Log(u1)); double temporaryValue2 = 2.0 * Math.PI * u2; this.nextGaussianValue = temporaryValue1 * Math.Sin(temporaryValue2); this.available = true; return temporaryValue1 * Math.Cos(temporaryValue2); } #endregion #region 난수 값 구하기 - GetRandomValue(mean, standardDeviation) /// <summary> /// 난수 값 구하기 /// </summary> /// <param name="mean">평균</param> /// <param name="standardDeviation">표준 편차</param> /// <returns>난수 값</returns> public double GetRandomValue(double mean, double standardDeviation) { return mean + standardDeviation * GetRandomValue(); } #endregion #region 난수 값 구하기 - GetRandomValue(standardDeviation) /// <summary> /// 난수 값 구하기 /// </summary> /// <param name="standardDeviation">표준 편차</param> /// <returns>난수 값</returns> public double GetRandomValue(double standardDeviation) { return standardDeviation * GetRandomValue(); } #endregion } |
▶ Program.cs
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 |
namespace TestProject; /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { GaussianRandom random = new GaussianRandom(); for(int i = 0; i < 10; i++) { double randomValue = random.GetRandomValue(10d, 3d); Console.WriteLine(randomValue); } } #endregion } |