Keyword: 負の二項分布, 整数疑似乱数ベクトル
概要
本サンプルは負の二項分布から整数疑似乱数ベクトルの生成を行うC#によるサンプルプログラムです。 本サンプルは確率が以下で表される、m=60、p=0.999の負の二項分布から20個の整数疑似乱数をを生成し出力します。
※本サンプルはnAG Library for .NETに含まれる関数 g05th() のExampleコードです。本サンプル及び関数の詳細情報は g05th のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
出力結果
(本関数の詳細はg05th のマニュアルページを参照)| この出力例をダウンロード |
g05th Example Program Results
62339
50505
64863
66289
50434
59461
57365
65965
59572
63104
47833
54735
62075
48018
61458
55190
54263
80995
70129
60200
- 4〜23行目に生成された整数疑似乱数が出力されています。
ソースコード
(本関数の詳細はg05th のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g05th Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
using System.IO;
namespace NagDotNetExamples
{
public class G05THE
{
static void Main(String[] args)
{
StartExample();
}
public static void StartExample()
{
try
{
const int mseed=1;
const int n=20;
const int lr=1;
double p=0.0;
int genid, i, m, subid;
double[] r = new double[lr];
int[] seed = new int[mseed];
int[] x = new int[n];
int ifail;
Console.WriteLine("g05th Example Program Results");
Console.WriteLine("");Console.WriteLine("");
// Set the distribution parameter P
p = 0.9990e0;
m = 60;
// Initialise the seed
seed[0] = 1762543;
// genid and subid identify the base generator
genid = 1;
subid = 1;
// Initialise the generator to a repeatable sequence
G05.G05State g05State = new G05.G05State(genid, subid, seed, out ifail);
if (ifail != 0)
{
Console.WriteLine("** Generator initialisation failed with ifail = {0,5}", ifail);
goto L20;
}
// Generate the variates
// Choose mode = 3 because p close to 1
G05.g05th(3, n, m, p,r, g05State, x, out ifail);
if (ifail != 0)
{
Console.WriteLine("** g05th failed with ifail = {0,5}", ifail);
goto L20;
}
// Display the variates
for (i = 1 ; i <= n ; i++)
{
Console.WriteLine(" {0,10}", x[i - 1]);
}
Console.WriteLine("");
//
L20: ;
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine( "Exception Raised");
}
}
}
}
