MathSrand

擬似ランダム整数の系列を生成するための開始点を設定します。

void  MathSrand(
  int  seed      // 数の初期化
  );

パラメータ

seed

[in]  乱数列の初値

戻り値

なし

注意事項

MathRand() 関数は、擬似乱数列を生成するために使用されます。特定の初期数を使用して MathSrand() を呼び出すことで、常に同じ擬似乱数列の生成が可能です。

GetTickCount()は、オペレーティングシステムの開始の瞬間から増加し内蔵ミリ秒のカウンタがオーバーフローするまで49日以内に繰り返されないので、非経常な列の受信を確保するには MathSrand(GetTickCount()) が使用されるべきです。TimeCurrent() 関数は最終ティックの時刻を戻し、週末などには長時間変わらないことがあるので、MathSrand(TimeCurrent()) の使用は不適切です。

指標及びエキスパートアドバイザーのための MathSrand() を使用した乱数発生器の初期化は、OnTick() 及び OnCalculate() 内での発生器の複数にわたった再起動を避けるために OnInit() ハンドラ内でよりよく行われます。

MathSrand() 関数の代わりに srand() が使用可能です。

例:

#property description "The indicator shows the central limit theorem, which states:"
#property description "The sum of a sufficiently large number of weakly dependent random variables, "
#property description "having approximately equal magnitude (none of the summands dominates,"
#property description "or makes a determining contribution to the sum), has a distribution close to normal."
 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- グラフィック構成のプロパティ
#property indicator_label1 "Label"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1 clrRoyalBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1  5
//--- 入力変数
input int     sample_number=10;
//--- 分布を描画するための指標バッファ
double         LabelBuffer[];
//--- ティックカウンタ
double         ticks_counter;
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数                                            |
//+------------------------------------------------------------------+
void OnInit()
 {
//--- 配列と指標バッファを関連付ける
  SetIndexBuffer(0,LabelBuffer,INDICATOR_DATA);
//--- 指標バッファを現在から過去に向ける
  ArraySetAsSeries(LabelBuffer,true);
//--- 擬似乱数生成器を初期化する
  MathSrand(GetTickCount());
//--- ティックカウンタを初期化する
  ticks_counter=0;
 }
//+------------------------------------------------------------------+
//| カスタム指標の反復関数                                                |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
              const int prev_calculated,
              const datetime &time[],
              const double &open[],
              const double &high[],
              const double &low[],
              const double &close[],
              const long &tick_volume[],
              const long &volume[],
              const int &spread[])
 {
//--- ゼロカウンタで指標バッファをリセットする
  if(ticks_counter==0) ArrayInitialize(LabelBuffer,0);
//--- カウンタを増加
  ticks_counter++;
//--- 定期的に配信を復活させるために、ティックカウンターをリセットする必要がある
  if(ticks_counter>100)
    {
    Print("We've reset the indicator values, let's start filling the cells once again");
     ticks_counter=0;
    }
//--- 0〜7の3つの数の和としてランダムな値のサンプルを取得する
  for(int i=0;i<sample_number;i++)
    {
    //--- 乱数が他の 3 つの数の和であるセルのインデックスの計算
    int rand_index=0;
    //--- 0〜7 の 3 つの乱数を取得
    for(int k=0;k<3;k++)
       {
        //--- 7 による除算で余りは 0〜6 の値を返す
        rand_index+=MathRand()%7;
       }
    //--- セル番号 rand_index の値を 1 で増やす
     LabelBuffer[rand_index]++;
    }
//--- OnCalculate() ハンドルを終了する
  return(rates_total);
 }