Keyword: ランダム2元配置表
概要
本サンプルはランダム2元配置表の生成を行うC#によるサンプルプログラムです。 本サンプルは行の合計が9、11、7、23、列の合計が16、17、17となる 4x3 のランダム2元配置表を生成し出力します。
※本サンプルはnAG Library for .NETに含まれる関数 g05pz() のExampleコードです。本サンプル及び関数の詳細情報は g05pz のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
出力結果
(本関数の詳細はg05pz のマニュアルページを参照)| この出力例をダウンロード |
g05pz Example Program Results 2 4 3 | 9 6 1 4 | 11 2 4 1 | 7 6 8 9 | 23 ------------+------ 16 17 17 | 50
- 4〜9行目に生成されたランダム2元配置表が出力されています。
ソースコード
(本関数の詳細はg05pz のマニュアルページを参照)
※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法
| このソースコードをダウンロード |
// g05pz Example Program Text
// C# version, nAG Copyright 2008
using System;
using NagLibrary;
using System.IO;
namespace NagDotNetExamples
{
public class G05PZE
{
static void Main(String[] args)
{
StartExample();
}
public static void StartExample()
{
try
{
const int lrow=4;
const int ncol=3;
const int mseed=1;
const int lr=60;
int genid, i, j, rctot, subid; double[] r = new double[lr];
int[] seed = new int[mseed];
int[] totc = new int[ncol];
int[] totr = new int[lrow];
int[,] x = new int[lrow, ncol];
int ifail;
Console.WriteLine("g05pz Example Program Results");
Console.WriteLine("");Console.WriteLine("");
// Set the table row and column totals
totr[0] = 9;
totr[1] = 11;
totr[2] = 7;
totr[3] = 23;
totc[0] = 16;
totc[1] = 17;
totc[2] = 17;
rctot = 50;
// 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 L40;
}
// Generate the random table
// Choose mode = 2
G05.g05pz(2, lrow, ncol, totr, totc,r, g05State, x, out ifail);
if (ifail != 0)
{
Console.WriteLine("** g05pz failed with ifail = {0,5}", ifail);
goto L40;
}
// Display the results
for (i = 1 ; i <= lrow ; i++)
{
for (j = 1 ; j <= ncol ; j++)
{
Console.Write("{0,3}", x[i - 1 , j - 1]);
}
Console.Write("{0,3} ", " | "+totr[i - 1]);
Console.WriteLine("");
}
Console.WriteLine("{0}", "------------+------");
for (j = 1 ; j <= ncol ; j++)
{
Console.Write("{0,3}", totc[j - 1]);
}
Console.Write("{0,3} ", " | " + rctot);
Console.WriteLine("");
//
L40: ;
//
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine( "Exception Raised");
}
}
}
}
