サンプルコード 一覧へ戻る

MQL5サンプルコード

【MT5(MQL5)サンプルコード】MathSrand

スポンサーリンク / Sponsored links

MT4(MQL4)/MT5(MQL5)のMathSrand

擬似乱数値を初期化するための処理です。

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

MT4→MT5になっても変わらず使えます。

初期化後、MathRandやrandが使用できるようになります。

サンプルコード

void OnInit()  
{
   MathSrand(GetTickCount());
   
   int randnum = 0;
   
   randnum = MathRand(); //0-32,767
   Print("randnum=",randnum);
   randnum = MathRand(); //0-32,767
   Print("randnum=",randnum);
   randnum = MathRand(); //0-32,767
   Print("randnum=",randnum);
   randnum = MathRand(); //0-32,767
   Print("randnum=",randnum);
   randnum = MathRand(); //0-32,767
   Print("randnum=",randnum);
   
   Print("-------------");
   
   randnum = MathRand() % 2; //0 or 1 
   Print("randnum=",randnum);
   randnum = MathRand() % 2; //0 or 1 
   Print("randnum=",randnum);
   randnum = MathRand() % 2; //0 or 1 
   Print("randnum=",randnum);
   randnum = MathRand() % 2; //0 or 1 
   Print("randnum=",randnum);
   randnum = MathRand() % 2; //0 or 1 
   Print("randnum=",randnum);
   
   Print("-------------");
   
   randnum = MathRand() % 50; //0 - 49 
   Print("randnum=",randnum);
   randnum = MathRand() % 50; //0 - 49 
   Print("randnum=",randnum);
   randnum = MathRand() % 50; //0 - 49 
   Print("randnum=",randnum);
   randnum = MathRand() % 50; //0 - 49 
   Print("randnum=",randnum);
   randnum = MathRand() % 50; //0 - 49 
   Print("randnum=",randnum);
   
   Print("-------------");
   
   randnum = rand();  //0-32,767
   Print("randnum=",randnum);
   randnum = rand();  //0-32,767
   Print("randnum=",randnum);
   randnum = rand();  //0-32,767
   Print("randnum=",randnum);
   randnum = rand();  //0-32,767
   Print("randnum=",randnum);
   randnum = rand();  //0-32,767
   Print("randnum=",randnum);
   
   Print("-------------");
   
   randnum = rand() % 2; //0 or 1 
   Print("randnum=",randnum);
   randnum = rand() % 2; //0 or 1 
   Print("randnum=",randnum);
   randnum = rand() % 2; //0 or 1 
   Print("randnum=",randnum);
   randnum = rand() % 2; //0 or 1 
   Print("randnum=",randnum);
   randnum = rand() % 2; //0 or 1 
   Print("randnum=",randnum);
   Print("-------------");
   
   randnum = rand() % 50; //0 - 49 
   Print("randnum=",randnum);
   randnum = rand() % 50; //0 - 49 
   Print("randnum=",randnum);
   randnum = rand() % 50; //0 - 49 
   Print("randnum=",randnum);
   randnum = rand() % 50; //0 - 49 
   Print("randnum=",randnum);
   randnum = rand() % 50; //0 - 49 
   Print("randnum=",randnum);
   
   return;
}

結果

randnum=8686
randnum=12639
randnum=32576
randnum=25586
randnum=16911
-------------
randnum=1
randnum=1
randnum=1
randnum=0
randnum=0
-------------
randnum=30
randnum=43
randnum=20
randnum=35
randnum=7
-------------
randnum=8997
randnum=5940
randnum=6307
randnum=24877
randnum=12282
-------------
randnum=1
randnum=1
randnum=1
randnum=1
randnum=1
-------------
randnum=47
randnum=33
randnum=34
randnum=46
randnum=35

コメント / Comments

タイトルとURLをコピーしました