//+------------------------------------------------------------------+ //| MathRand_no_bias.mq5 | //| Copyright © 2018, Amr Ali | //| https://www.mql5.com/en/users/amrali | //+------------------------------------------------------------------+ #property description "Random number generation using MQL's MathRand() function." //+------------------------------------------------------------------+ //| Random double in the range [min, max) | //| Generates doubles rounded down to the nearest multiple of 1/2^15 | //+------------------------------------------------------------------+ double MathRandDbl(const double min, const double max) { double f = (MathRand() / 32768.0); return min + (f * (max - min)); } //+------------------------------------------------------------------+ //| Random integer in the range [min, max) | //| Generates random numbers with a uniform distribution (bias-free).| //| http://www.pcg-random.org/posts/bounded-rands.html | //+------------------------------------------------------------------+ int MathRandInt(const int min, const int max) { int range = max - min; int limit = (32768 - range) % range; int r; do{ r = MathRand(); }while (r < limit); return r % range + min; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnStart() { //--- Initialize the generator of random numbers MathSrand(GetTickCount()^(uint)ChartID()); //--- Generate and print some random dates Print("Random dates from 1990.01.01 till today: "); for(int i=0; i<7; i++) { double rnd=MathRandDbl(D'1990.01.01',TimeCurrent()); Print((datetime)rnd); } //--- check disribution histogram (frequency table) int hist[]; int bound=13; int sample=100000; ArrayResize(hist,bound); ZeroMemory(hist); for(int i=0; i