正規準乱数列の生成

C言語によるサンプルソースコード : 使用関数名:nag_quasi_rand_normal (g05yjc)

ホーム > 製品 > nAG数値計算ライブラリ > サンプルソースコード集 > 正規準乱数列の生成 (C言語/C++)

Keyword: 正規準乱数列

概要

本サンプルは正規準乱数列の生成を行うC言語によるサンプルプログラムです。 本サンプルは正規分布から準乱数列を生成し出力します。

※本サンプルはnAG Cライブラリに含まれる関数 nag_quasi_rand_normal() のExampleコードです。本サンプル及び関数の詳細情報は nag_quasi_rand_normal のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで

出力結果

(本関数の詳細はnag_quasi_rand_normal のマニュアルページを参照)
1
2
3
4
5
6
7

この出力例をダウンロード
nag_quasi_rand_normal (g05yjc) Example Program Results

     1.5820    2.2448    0.9154    3.0722
     2.8768    1.6057    3.7341    5.4521
     0.9240    3.0223    2.3828    3.8154
     0.6004    1.9290    1.9355    3.4806
     2.0141    3.9061    3.3680    4.8479

  • 3~7行目に生成された5個の4次元の準乱数列が出力されています。

ソースコード

(本関数の詳細はnag_quasi_rand_normal のマニュアルページを参照)

※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

このソースコードをダウンロード
/* nag_quasi_rand_normal (g05yjc) Example Program.
 *
 * CLL6I261D/CLL6I261DL Version.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

/* Pre-processor includes */
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg05.h>
#define QUAS(I, J) quas[(order == Nag_ColMajor)?(J*pdquas + I):(I*pdquas + J)]

int main(void)
{
  /* Integer scalar and array declarations */
  Integer exit_status = 0;
  Integer liref, i, j, q_size;
  Integer *iref = 0;
  Integer pdquas;

  /* nAG structures */
  NagError fail;

  /* Double scalar and array declarations */
  double *quas = 0;

  /* Number of dimensions */
  Integer idim = 4;

  /* Mean and standard deviation of the normal distribution */
  double xmean[] = { 1.0e0, 2.0e0, 3.0e0, 4.0e0 };
  double std[] = { 1.0e0, 1.0e0, 1.0e0, 1.0e0 };

  /* Set the sample size */
  Integer n = 5;

  /* Skip the first 1000 variates */
  Integer iskip = 1000;

  /* Use column major order */
  Nag_OrderType order = Nag_ColMajor;

  /* Choose the quasi generator */
  Nag_QuasiRandom_Sequence genid = Nag_QuasiRandom_Sobol;

  /* Initialize the error structure */
  INIT_FAIL(fail);

  printf("nag_quasi_rand_normal (g05yjc) Example Program Results\n\n");

  pdquas = (order == Nag_RowMajor) ? idim : n;
  q_size = (order == Nag_RowMajor) ? pdquas * n : pdquas * idim;

  /* Calculate the size of the reference vector */
  liref = (genid == Nag_QuasiRandom_Faure) ? 407 : 32 * idim + 7;

  /* Allocate arrays */
  if (!(quas = nAG_ALLOC(q_size, double)) ||
      !(iref = nAG_ALLOC(liref, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Initialize the Sobol generator */
  nag_quasi_init(genid, idim, iref, liref, iskip, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_quasi_init (g05ylc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Generate a normal quasi-random number sequence */
  nag_quasi_rand_normal(order, xmean, std, n, quas, pdquas, iref, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_quasi_rand_normal (g05yjc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print the estimated value of the integral */
  for (i = 0; i < n; i++) {
    printf("  ");
    for (j = 0; j < idim; j++)
      printf("%9.4f%s", QUAS(i, j), ((j + 1) % 4) ? " " : "\n");
    if (idim % 4)
      printf("\n");
  }

END:
  nAG_FREE(quas);
  nAG_FREE(iref);

  return exit_status;
}


関連情報
© 日本ニューメリカルアルゴリズムズグループ株式会社 2025
Privacy Policy  /  Trademarks