Why does MathRand keep rounding?

 

I have a variable...


Double randomnumber1 = MathRand()/32600;


But randomnumber1 keeps coming back as 0.0 even though MathRand is choosing random numbers between 0 and 32600!! I just want a decimal number between 0 and 1. JEEEEESUS.


I've spent so long on trying to get this one line to work. How is it done?

 
double randomnumber1 = MathRand()/32600.;
 
Alain Verleyen:


OMG it works. Why is that little dot not in the literature?

I just managed to get it to work myself by doing:

Double randomnumber1 = (MathRand()+0.000000000)/32600;


Cheers

 
Robert Dufton:


OMG it works. Why is that little dot not in the literature?

I just managed to get it to work myself by doing:

Double randomnumber1 = (MathRand()+0.000000000)/32600;


Cheers


It is in the literature, just a little hidden away: https://docs.mql4.com/basis/types/casting

Forum on trading, automated trading systems and testing trading strategies

Before operations (except for the assignment ones) are performed, the data are converted into the maximum priority type. Before assignment operations are performed, the data are cast into the target type.

Examples:

   int    i=1/2;        // no types casting, the result is 0
   Print("i = 1/2  ",i);
 
   int k=1/2.0;         // the expression is cast to the double type,
   Print("k = 1/2  ",k);  // then is to the target type of int, the result is 0
 
   double d=1.0/2.0;    // no types casting, the result is 0.5
   Print("d = 1/2.0; ",d);
 
   double e=1/2.0;      // the expression is cast to the double type,
   Print("e = 1/2.0; ",e);// that is the same as the target type, the result is 0.5
 
   double x=1/2;        // the expression of the int type is cast to the double target typr,
   Print("x = 1/2; ",x);  // the result is 0.0

Your code was like the example highlighted in yellow.

Alain's was like the example highlighted in green.

Typecasting - Data Types - Language Basics - MQL4 Reference
Typecasting - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Typecasting - Data Types - Language Basics - MQL4 Reference