# create a random number from -1.0 to 1.0

324
2014.06.24 08:29

Hi guys,

I just bump in to MathRand() function but it only calculates positive number. Do you have any idea on how to get random double numbers from -1.0 to 1.0?

Thanks...

1401
2014.06.24 08:58

To get negative numbers, try using minus in the formula, such as

```var1 = mathrandoutput - 1.2; // anything bigger than the output
```
241
2014.06.24 09:32
I can show you how to generate two random numbers-1.0 and 1.0. According to the principle: even/odd.

If the  MathRand() is even number, will be 1.0. If the MathRand() is odd number, then will-1.0.

```//+------------------------------------------------------------------+
//|                                                  randomness .mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property show_confirm
#property version   "1.00"
#property strict
int q=10;  // quantity of numbers
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
//---
int i;
double y,x,z,s[];
ArrayResize(s,q,q);

for(i=0; i<q; i++)
{
x=MathRand();
y=x/2;
z=y-MathFloor(y);
s[i]=1.0;
if(z>0)s[i]=-1.0;  //if x is odd number, then z>0.
}
}
//+------------------------------------------------------------------+```
324
2014.06.24 09:36
Boeing747:
I can show you how to generate two random numbers-1.0 and 1.0. According to the principle: even/odd.

If the  MathRand() is even number, will be 1.0. If the MathRand() is odd number, then will-1.0.

thanks but i would like it to have values from -1 to 1 like 0.5, -0.2 and so on...
Moderator
2413
2014.06.24 11:03
`double RanNum = (2*MathRand()/32767.0)-1;`

I'm sure somebody is going to show up to explain why that isn't entirely random...

241
2014.06.24 12:38

may be so?

```//+------------------------------------------------------------------+
//|                                                  randomness .mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property show_confirm
#property version   "1.00"
#property strict
int q=10;  // quantity of numbers
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
//---
double v[]={-1.0,-0.9,-0.8,-0.7,-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0};
int i,e;
double y=0;
double c=32767.0/21;
int z=0;
int x;

for(e=0; e<q; e++)
{
x=MathRand();
for(i=1; i<=20; i++)
{
z=(int)MathRound(i*c);
y=v[i-1];
if(z>=x)break;
y=v[i];
}
Sleep(100);
 raven.chrono: I just bump in to MathRand() function but it only calculates positive number. Do you have any idea on how to get random double numbers from -1.0 to 1.0? ```double Random(double max, double min=0.({   return min + (max - min) * MathRandom() / 32767.; } int Random(int max, int min=0({   return min + (max - min + 1) * MathRandom() / 32768.; } bool Coin(){ return MathRandom() >= 16384```