-8

乱数を作成してから、見つけたこのボックス ミュラー アルゴリズムを使用しようとしています。私が直面している問題は、System.Random 値を使用してあらゆる種類の数学演算を行うことです。平方根やログを取得したり、浮動小数点値と混ぜたりすることはできません。このランダム分布のコードは次のとおりです。数日間考えましたが、何も思いつきません。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        Random rand1 = new Random();
        Console.WriteLine("999 Doubles1.");
        for (int ctr = 0; ctr <= 999; ctr++)
            Console.Write("{0,8:N3}", rand1.NextDouble());
        Console.WriteLine();
        Random rand2 = new Random();
        Console.WriteLine("999 Doubles2.");
        for (int ctr = 0; ctr <= 999; ctr++)
            Console.Write("{0,8:N3}", rand2.NextDouble());
        Console.WriteLine();

        float mu = .75F;
        float sigma = .1F;

        float z1 = Math.Sqrt(-2 * Math.Log(rand1)) * Math.Sin(2 * Math.PI * rand2);
        float z2 = Math.Sqrt(-2 * Math.Log(rand1)) * Math.Cos(2 * Math.PI * rand2);
        float x1 = mu + z1 * sigma;
        float x2 = mu + z2 * sigma;
        }
    }
}
4

2 に答える 2

8

このコードを見てください:

 Math.Log(rand1)

それは、乱数のログを取得しようとしているのではありません...乱数ジェネレーターのログを取得しようとしています。次のようなものが必要です:

double randomNumber = rand1.NextDouble();
// Code using Math.Log(randomNumber)

乱数発生器自体で数値演算を実行するという概念はあまり意味がありません。

于 2013-04-17T15:11:51.743 に答える