Random number generator within range

 
Is it possible within MQL4 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?

Regards,
Loren
 
I think I figured it out. I'd like to recommend that this function be added to MetaTrader. I'm using it with SetGlobalVariableOnCondition() function to sleep for a random amount of time instead of a fixed amount of time.

//+------------------------------------------------------------------+
//| Return a random number betwen x and y                       |
//+------------------------------------------------------------------+
double MathRandRange(double x, double x)
{
   return(x+MathMod(MathRand(),MathAbs(x-y)));
}



Regards,
Loren

 
Oops, the declaration should read like this:
Removed because another silly error.  See code in next message.



Regards,
Loren

 
Dang it, if I could hit the right letter, this would work better.

double MathRandRange(double x, double y)
 
You need to start randomization in the init function. see MathSrand function.
there is no problem with range. our range is known - from 0 to 32767. normalize to yor range.
for instance:
double rand1= MathRand()/32767.0;
double rand2=rand1/(high_range-low_range) + low_range;
or so on.
 
Sure, that's more or less what the function I wrote does. I just thought it would be nice if the function were included as part of MetaTrader so people wouldn't have to figure out how to get a random number in a certain range.

It's also a lot easier to read and understand the code when you just see a function call. Trying to figure out what the formula does is not always this easy. And some people can't figure out the formula at all. Alienating those people seems silly when the solution is adding a simple two line function to the platform.

btw, Thanks for the semaphore code. That was useful. I'd also recommend that that become a suite of functions. One to initialize, one to lock, and one to unlock the semaphore. But I already wrote my functions, so it's not my problem if you guys don't see the value of including such simple functions.

Regards,
Loren
 
my mistake. second line should be
double rand2=rand1*(high_range-low_range) + low_range

there are 2 lines of source. separate function is not needed. write effective programs - function call is too expensive vs 2 lines of simple calculations
Reason: