Keyword: 論理疑似乱数
概要
本サンプルは論理疑似乱数ベクトルの生成を行うC#によるサンプルプログラムです。 本サンプルはTRUE(真)となる確率が50%の場合の論理疑似乱数20個を生成し出力します。
※本サンプルはnAG Library for .NETに含まれる関数 g05tb() のExampleコードです。本サンプル及び関数の詳細情報は g05tb のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
出力結果
(本関数の詳細はg05tb のマニュアルページを参照)| この出力例をダウンロード |
g05tb Example Program Results
False
True
False
False
True
True
True
False
True
False
True
True
False
True
False
True
True
False
False
False
- 3〜22行目に生成された論理疑似乱数が出力されています。
ソースコード
(本関数の詳細はg05tb のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g05tb Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
using System.IO;
namespace NagDotNetExamples
{
public class G05TBE
{
static void Main(String[] args)
{
StartExample();
}
public static void StartExample()
{
try
{
const int mseed=1;
const int n=20;
double p=0.0;
int genid, i, subid;
int[] seed = new int[mseed];
bool[] x = new bool[n];
int ifail;
Console.WriteLine("g05tb Example Program Results");
Console.WriteLine("");
// Set the distribution parameters P
p = 0.50e0;
// 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
G05.g05tb(n, p, g05State, x, out ifail);
if (ifail != 0)
{
Console.WriteLine("** g05tb failed with ifail = {0,5}", ifail);
goto L20;
}
// Display the variates
for (i = 1 ; i <= n ; i++)
{
Console.WriteLine(" {0,8:f4}", x[i - 1]);
}
Console.WriteLine("");
//
L20: ;
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine( "Exception Raised");
}
}
}
}
