跳至内容

MathSrand

MathSrand

设置生成一系列伪随机整数的起始点。

void  MathSrand(
   int  seed      // initializing number
   );

参数

seed

[in] 随机数序列的起始数字。

返回值

无返回值。

注意

使用MathRand()函数生成伪随机数序列。调用MathSrand()并指定一个初始值可以始终产生相同的伪随机数序列。

为了确保获得不重复的序列,可以使用MathSrand(GetTickCount()),因为GetTickCount()的值从操作系统启动那一刻起开始增加,并且在49天内不会重复,直到内置的毫秒计数器溢出为止。使用MathSrand(TimeCurrent())不太合适,因为TimeCurrent()函数返回的是最后一次计数的时间,这可能在很长一段时间内保持不变,例如在周末。

使用MathSrand()对指示器和专家顾问进行随机数生成器的初始化最好在OnInit()处理程序中完成;这样可以避免在OnTick()和OnCalculate()中多次重新启动生成器。

可以使用srand()函数代替MathSrand()函数。

示例:

#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
//--- Properties of the graphical construction
#property indicator_label1  "Label"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrRoyalBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  5
//--- An input variable
input int      sample_number=10;
//--- An indicator buffer to for drawing the distribution
double         LabelBuffer[];
//--- A counter of ticks
double         ticks_counter;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- Binding an array and an indicator buffer
   SetIndexBuffer(0,LabelBuffer,INDICATOR_DATA);
//--- turn the indicator buffer around from the present to the past
   ArraySetAsSeries(LabelBuffer,true);
//--- Initialize the generator of random numbers
   MathSrand(GetTickCount());
//--- Initialize the counter of ticks
   ticks_counter=0;
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//--- For a zero counter reset the indicator buffer
   if(ticks_counter==0) ArrayInitialize(LabelBuffer,0);
//--- Increase the counter
   ticks_counter++;
//--- We should periodically reset the counter ticks, to revive the distribution
   if(ticks_counter>100)
     {
      Print("We've reset the indicator values, let's start filling the cells once again");
      ticks_counter=0;
     }
//--- Get a sample of random values as the sum of three numbers from 0 to 7
   for(int i=0;i<sample_number;i++)
     {
      //--- Calculation of the index of the cell, where the random number falls as the sum of three other numbers
      int rand_index=0;
      //--- Get three random numbers from 0 to 7
      for(int k=0;k<3;k++)
        {
         //--- A remainder in the division by 7 will return a value from 0 to 6
         rand_index+=MathRand()%7;
        }
      //--- Increase the value in the cell number rand_index by 1
      LabelBuffer[rand_index]++;
     }
//--- Exit the OnCalculate() handler
   return(rates_total);
  }
最后更新于