Random number generator

 

Hi my friends, I'm trying to creat an indicator that simply generates a random integer < rank (as an input) & that random number doesn't change every tick but it did'nt work, please help me. 

//--- input parameters
input int      Rank=3;

//--- indicator buffers
double   RandBuf[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,RandBuf,INDICATOR_DATA);
   
//initialize the generator of random numbers
   MathSrand(GetTickCount());

 
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---
   int randnum=MathRand()%Rank;
   RandBuf[0]=randnum;
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
Tuan Nghia Phan:

Hi my friends, I'm trying to creat an indicator that simply generates a random integer < rank (as an input) & that random number doesn't change every tick but it did'nt work, please help me. 

The range of generated numbers are {0, 1, 2}, so, there is 33.3% possibility that you get the same number twice in a row.

Try increasing the range by using a larger value for "Rank".