Keyword: 整数ベクトル, 疑似ランダムサンプリング
概要
本サンプルは整数ベクトルの疑似ランダムサンプリングを行うC#によるサンプルプログラムです。 本サンプルは1から8までの8個の昇順の整数を含むベクトルからランダムにサンプルサイズ1から8までのサンプリングを行い、その結果を出力します。
※本サンプルはnAG Library for .NETに含まれる関数 g05nd() のExampleコードです。本サンプル及び関数の詳細情報は g05nd のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
出力結果
(本関数の詳細はg05nd のマニュアルページを参照)| この出力例をダウンロード |
g05nd Example Program Results Samples from the first 8 integers Sample size Values 1 2 2 3 6 3 1 5 7 4 2 6 7 8 5 1 2 3 4 8 6 1 3 4 5 6 7 7 1 3 4 5 6 7 8 8 1 2 3 4 5 6 7 8
- 3行目に8個の整数からのサンプリングであることが示されています。
- 7〜14行目にサンプルサイズとサンプリングされた値が出力されています。
ソースコード
(本関数の詳細はg05nd のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g05nd Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
using System.IO;
namespace NagDotNetExamples
{
public class G05NDE
{
static void Main(String[] args)
{
StartExample();
}
public static void StartExample()
{
try
{
const int mseed=1;
const int n=8;
int genid, i, m, subid; int[] ipop = new int[n];
int[] isampl = new int[n];
int[] seed = new int[mseed];
int ifail;
Console.WriteLine("g05nd Example Program Results");
Console.WriteLine("");
// 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 L60;
}
//
Console.WriteLine(" {0}{1,1}{2}"," Samples from the first ",n," integers");
Console.WriteLine("");
Console.WriteLine(" {0}", " Sample size\tValues" + "\r\n");
// Initialise the population
for (i = 1 ; i <= n ; i++)
{
ipop[i - 1] = i;
}
// Generate samples of different sizes
for (m = 1 ; m <= n ; m++)
{
G05.g05nd(ipop, n, isampl, m, g05State, out ifail);
if (ifail != 0)
{
Console.WriteLine("** g05nd failed with ifail = {0,5}", ifail);
goto L60;
}
// Display the results
Console.Write("\t {0}", m+"\t");
for (i = 1 ; i <= m ; i++)
{
Console.Write(" {0}", isampl[i - 1]);
}
Console.WriteLine("");
}
//
L60: ;
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine( "Exception Raised");
}
}
}
}
