//+------------------------------------------------------------------+
//|                                           NormalDistribution.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.ru/"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DarkBlue
#property indicator_minimum 0
//---- input parameters
extern int       N=1000;
extern int       range=100;
extern int       pile=5;
bool Calculated=false;
//---- buffers
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,ExtMapBuffer1);
   double Max,Min;
   int firstBar,lastBar;
   int range=WindowBarsPerChart();
   firstBar=WindowFirstVisibleBar();
   MathSrand(GetTickCount());
   IndicatorShortName(StringConcatenate("N=",N,"  pile=",pile,"       "));
//----
   return(0);
  }

//+------------------------------------------------------------------+
//|  получим члучайное число от 0 до Maximum                         |
//+------------------------------------------------------------------+
double Random(double Maximum)
   {
   int res;
//----
   res=MathRand()/32767.0*Maximum;
//----
   return(res);   
   }
//+------------------------------------------------------------------+
//|  получим среднее от NumberOfRandom чисел                         |
//+------------------------------------------------------------------+
double GetAverage(int MaxRandom,int NumberOfRandom)
   {
   double res;
   int i;
//----
   for (i=0;i<NumberOfRandom;i++)
      {
      res+=Random(MaxRandom);
      } 
//----
   return(res/NumberOfRandom);   
   }

//+------------------------------------------------------------------+
//| заполним секции частотного распределения                         |
//+------------------------------------------------------------------+
void FillSections(int & Array[],int Range,int Pile,int Balls, double & max)
   {
   int i,index;
//----
   for (i=0;i<Balls;i++)
      {
      index=GetAverage(Range,Pile);
      Array[index]++;
      }

   for (i=0;i<Balls;i++) if (Array[i]>max) max=Array[i];

//----
   return;   
   }
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int   i, counted_bars=IndicatorCounted();
   double maxDistribution;
//----
   int section[];
   ArrayResize(section,range);
   if (!Calculated)
      {
      FillSections(section,range,pile,N,maxDistribution);
      for (i=0;i<range;i++)
         {
         ExtMapBuffer1[i]=section[i]/maxDistribution;
         }
      Calculated=true;
      }
//----
   return(0);
  }
//+------------------------------------------------------------------+