Random Numbers Below 1

 

Hi Community,

I’m looking to generate random numbers between 0.4 and 0.5. What’s best way to use the MathRand() function to do this?

Cheers, Tim
 

If you had used this, you would have found this: MathRand between 3000 and 5000 - Forex Trading Tutorial - Expert Advisors and Automated Trading - MQL5 programming forum (two weeks ago) and RandomLinear (2019)

RandomLinear(30000)/300000.0+0.4; // [0.40000 … 0.4999966666666667] increments of 0.000003333
RandomLinear(10000)/100000.0+0.4; // [0.40000 … 0.49999] increments of 0.00001
RandomLinear(5000,4000)/10000;    // [0.40000 … 0.49999] increments of 0.0001
 
Timothy Smith:

Hi Community,

I’m looking to generate random numbers between 0.4 and 0.5. What’s best way to use the MathRand() function to do this?

Cheers, Tim

MathRand()  returns a pseudorandom integer within the range of 0 to 32767.

Number between 0.4 and 0.5 is: 

double Number = 0.4 + MathRand() * (0.5 - 0.4) / 32767.;

 
Antonio Simon Del Vecchio #:

MathRand()  returns a pseudorandom integer within the range of 0 to 32767.

Number between 0.4 and 0.5 is: 

Thanks for your help Antonio, this community is awesome.
 
Thanks for your help with this William, much appeciated!
 
Antonio Simon Del Vecchio #: Number between 0.4 and 0.5 is: 
double Number = 0.4 + MathRand() * (0.5 - 0.4) / 32767.;

  1. Read the links I provided. Your code does not generate numbers of equal frequency. There is only a 1/32768 (.00003) chance of 0.5. There is a 3/32767 (.000091) chance of 0.4
  2. It also generates values between [0.4 and 0.5] inclusive. Inclusive was not specified, like a clock 0000…2400 does not include 2400.
Reason: