next up previous contents
Next: randn Up: リファレンスマニュアル Previous: qz

  
rand

【目的】
rand - 一様乱数
【形式】
y = rand()            Y = rand(n)
   Real y;               Array Y;
                         Integer n;
  
Y = rand(m,n)         Y = rand(X)
   Array Y;              Array Y;
   Integer m,n;          (Matrix|Array) X;
  
s = rand("seed");     p = rand("seed", s);
   Integer s;            Integer p;
                         Integer s;
【詳細】
rand()は,区間(0,1)に一様に分布する乱数を生成する。整数 n について, rand(n)は n * n の一様乱数配列を返す。rand(m,n) は,m * n の一様乱数 配列を返す。X が行列のとき,rand(X) は,X と同じ大きさの一様乱数 配列を返す。 rand() は疑似乱数に基づき区間 [2-53, 1-2-53] の浮動小数点を 生成する。理論的に,生成される乱数の周期は約 $2.3 ¥times 10^{18}$である。 乱数列は乱数発生器の状態に依存して決定され,乱数発生器の状態は 種によって変更できる。rand("seed") は一様乱数の種(Integer)を返し, rand("seed", m) は一様乱数の種を整数 m に設定する。
【例題】
1 * 4 の一様乱数配列を生成する。
Y = rand(1, 4)
=== [Y] : (  1,  4) ===
           (  1)         (  2)         (  3)         (  4)
(  1)  2.853808E-01  2.533581E-01  9.346853E-02  6.084968E-01
次の例では,まず乱数発生器の種を調べる。1個の乱数を生成すると種が 変わることが分かる。種を 1 に戻すと前と同じ乱数が生成される。
>> s = rand("seed")
s = 1271983233
>> y = rand()
y = 0.90342
>> s = rand("seed")
s = 1776642162
>> s = rand("seed", 1)
s = 1
>> y = rand()
y = 0.285381
【算法】
文献 1, 2 参照。
【参照】
randn(2.173)
【文献】
1.
William H. Press and Brian P. Flannery and Saul A. Teukolsky and William T. Veterling, Numerical Recipes in C (The Art of Scientific Computing), Cambridge University Press, 1988
2.
丹慶 勝市, 奥村 晴彦, 佐藤 俊朗, 小林 誠, ニューメリカルレシピ・イン・シー, 技術評論社, 1993


Masanobu KOGA 平成11年10月2日