Download MetaTrader 5

Random number generator within range in mql5

To add comments, please log in or register
We have launched Spanish and Portuguese versions of the website. Try them!
hopokuk 2015.05.10 02:01 
Is it possible within MQL5 to generate a random number between a set starting point and ending point? Let's say I want a random number between 1000 and 2000. How would I do that?
Mobarak Ali
Mobarak Ali 2015.05.10 04:29  

You can use this code:

int  RandomNumber;

for(int i = 0; i< 1; i++) {
    RandomNumber = MathRand();
    if(RandomNumber > 2000 || RandomNumber < 1000) {

Have a nice day!

Dr.Trader 2015.05.10 09:56  

You can use interpolation to adjust result to required bounds:

int MathRandomBounds(int minValue, int maxValue){
   return minValue + MathRound((maxValue-minValue)*(MathRand()/32767.0));

32767.0 is a constant maximal value for MathRand().

To add comments, please log in or register