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