Download MetaTrader 5
To add comments, please log in or register
Sign up for MQL5.community to leave your comments!
raven.chrono
324
raven.chrono 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...

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 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
Boeing747
241
Boeing747 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 copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#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. 
      Alert(s[i]);
     }
  }
//+------------------------------------------------------------------+
raven.chrono
324
raven.chrono 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...
honest_knave
969
honest_knave 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...

Boeing747
241
Boeing747 2014.06.24 12:38  

may be so?


//+------------------------------------------------------------------+
//|                                                  randomness .mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property show_confirm
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#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);
      Alert("Our number= ",y,"      MathRand()= ",x);
     }

  }
//+------------------------------------------------------------------+
whroeder1
13627
whroeder1 2014.06.24 16:50  
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
/
To add comments, please log in or register