■ 가우스 정규 난수기를 사용하는 방법을 보여준다.
▶ 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 |
namespace TestProject; /// <summary> /// 가우스 난수기 /// </summary> public sealed class GaussianRandom { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 난수기 /// </summary> private readonly Random random; /// <summary> /// 편차 보유 여부 /// </summary> private bool hasDeviation; /// <summary> /// 저장 편차 /// </summary> private double storedDeviation; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - GaussianRandom(random) /// <summary> /// 생성자 /// </summary> /// <param name="random">난수기</param> public GaussianRandom(Random random = null) { this.random = random ?? new Random(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 다음 가우스 난수 구하기 - NextGaussian(mean, standardDeviation) /// <summary> /// 다음 가우스 난수 구하기 /// </summary> /// <param name="mean">평균</param> /// <param name="standardDeviation">표준 쳔차</param> /// <returns>다음 가우스 난수</returns> public double NextGaussian(double mean = 0, double standardDeviation = 1) { if(standardDeviation <= 0) { throw new ArgumentOutOfRangeException("standardDeviation", "Must be greater than zero."); } if(this.hasDeviation) { this.hasDeviation = false; return this.storedDeviation * standardDeviation + mean; } double randomValue1; double randomValue2; double rSquared; do { randomValue1 = 2 * this.random.NextDouble() - 1; randomValue2 = 2 * this.random.NextDouble() - 1; rSquared = randomValue1 * randomValue1 + randomValue2 * randomValue2; } while(rSquared >= 1 || rSquared == 0); double polarTransformationValue = Math.Sqrt(-2 * Math.Log(rSquared) / rSquared); this.storedDeviation = randomValue2 * polarTransformationValue; this.hasDeviation = true; return randomValue1 * polarTransformationValue * standardDeviation + mean; } #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.NextGaussian(10d, 3d); Console.WriteLine(randomValue); } } #endregion } |