MathSrand

Sets the starting point for generating a series of pseudorandom integers.

void MathSrand(intseed// initializing number );

Parameters

seed

[in] Starting number for the sequence of random numbers.

Return Value

No return value.

Note

The MathRand() function is used for generating a sequence of pseudorandom numbers. Call of MathSrand() with a certain initializing number allows to always produces the same sequence of pseudorandom numbers.

To ensure receipt of non-recurring sequence, use the call of MathSrand(GetTickCount()), since the value of GetTickCount() increases from the moment of the start of the operating system and is not repeated within 49 days, until the built-in counter of milliseconds overflows. Use of MathSrand(TimeCurrent()) is not suitable, because the TimeCurrent() function returns the time of the last tick, which can be unchanged for a long time, for example at the weekend.

Initialization of the random number generator using MathSrand() for indicators and Expert Advisors is better performed in the OnInit() handler; it saves you from the following multiple restarts of the generator in OnTick() and OnCalculate().

Instead of the MathSrand() function you can use the srand() function.

Example:

#propertydescription"The indicator shows the central limit theorem, which states:"#propertydescription"The sum of a sufficiently large number of weakly dependent random variables, "#propertydescription"having approximately equal magnitude (none of the summands dominates,"#propertydescription"or makes a determining contribution to the sum), has a distribution close to normal."#propertyindicator_separate_window#propertyindicator_buffers 1#propertyindicator_plots 1//--- Properties of the graphical construction#propertyindicator_label1"Label"#propertyindicator_type1DRAW_HISTOGRAM#propertyindicator_color1clrRoyalBlue#propertyindicator_style1STYLE_SOLID#propertyindicator_width1 5//--- An input variableinputintsample_number=10;//--- An indicator buffer to for drawing the distributiondouble LabelBuffer[];//--- A counter of ticksdouble ticks_counter;//+------------------------------------------------------------------+//| Custom indicator initialization function |//+------------------------------------------------------------------+voidOnInit() {//--- Binding an array and an indicator bufferSetIndexBuffer(0,LabelBuffer,INDICATOR_DATA);//--- turn the indicator buffer around from the present to the pastArraySetAsSeries(LabelBuffer,true);//--- Initialize the generator of random numbersMathSrand(GetTickCount());//--- Initialize the counter of ticks ticks_counter=0; }//+------------------------------------------------------------------+//| Custom indicator iteration function |//+------------------------------------------------------------------+intOnCalculate(constint rates_total,constint prev_calculated,const datetime &time[],constdouble &open[],constdouble &high[],constdouble &low[],constdouble &close[],constlong &tick_volume[],constlong &volume[],constint &spread[]) {//--- For a zero counter reset the indicator bufferif(ticks_counter==0) ArrayInitialize(LabelBuffer,0);//--- Increase the counter ticks_counter++;//--- We should periodically reset the counter ticks, to revive the distributionif(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 7for(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 numbersint rand_index=0;//--- Get three random numbers from 0 to 7for(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() handlerreturn(rates_total); }