Download MetaTrader 5

How to build a random number

To add comments, please log in or register
Frika
37
Frika  

Hello,

I want to have a random number between 280 and 340 at the beginning of my EA.‌ So, I tried to make it in the onInit()
But they have all the same value when I have, maybe, 5 charts with the same EA and I restart Metatrader and they load all at once.

So, how can I build for each chart a random number even though I load the charts at once. ‌(at program start for example)

‌Thank you ver much

honest_knave
Moderator
2342
honest_knave  

MathRand

M‌athSrand

S‌ome detail here

S‌ome code here 

Documentation on MQL5: Math Functions / MathRand
Documentation on MQL5: Math Functions / MathRand
  • www.mql5.com
Math Functions / MathRand - Reference on algorithmic/automated trading language for MetaTrader 5
Frika
37
Frika  

I tried to make a random number. But it seems that the MathRand() has to program start the same initial valu‌e.

The problem is that I don´t wanna have the same number in each chart at the beginning of program start.

And if I have 10 chart with the same EA, at the program start they calculate all the same number.‌

int OnInit()
  {

   int tim = ceil((MathRand()%10001/100.0)+280);

   EventSetTimer(tim);     // number of seconds

‌The timer should have different values in each chart

honest_knave
Moderator
2342
honest_knave  
   srand(GetTickCount());
   int random_no = 280+(340-280)*rand()/32768;
Frika
37
Frika  

Unfortunately still the same number in each chart.

I thought I could take the Chart ID for doing some different stuff. But how can I take such a large number to get a proper result‌

got it. Thank you very much.

srand(ChartID());
int random_no = 280+(340-280)*rand()/32768

Documentation on MQL5: Chart Operations / ChartID
Documentation on MQL5: Chart Operations / ChartID
  • www.mql5.com
Chart Operations / ChartID - Reference on algorithmic/automated trading language for MetaTrader 5
honest_knave
Moderator
2342
honest_knave  
Frika:

Unfortunately still the same number in each chart.

I thought I could take the Chart ID for doing some different stuff. But how can I take such a large number to get a proper result‌

got it. Thank you very much.


You'll need to cast ChartID into an int to avoid a compiler warning.

G‌lad you got it sorted.

whroeder1
17063
whroeder1  
Frika: I want to have a random number between 280 and 340
int random_int(int end, int beg=0){ // Return a random number in the range [b, e).
   return int( beg + (end - beg) * MathRand() / 32768.);
}
  1. If you mean "between" (i.e. (280 .. 340) or [281 .. 339]) use random_int(281, 340)
    If you mean inclusive (i.e. [280 .. 340]) use random_int(280, 341)


honest_knave:
srand(GetTickCount());
int random_no = 280+(340-280)*rand()/32768;
  1. This returns [280 .. 339], and because it doesn't use floating point, the last number is not as frequent as the others.
  2. Call MathSrand only once.
honest_knave
Moderator
2342
honest_knave  

whroeder1:

  1. This returns [280 .. 339], and because it doesn't use floating point, the last number is not as frequent as the others.

Agreed.

whroeder1:

2. Call MathSrand only once.

‌The OP only generates the number once, in OnInit() to set a timer.

Carl Schreiber
Moderator
7943
Carl Schreiber  

what about:

srand(GetTickCount()); // in OnInit()
...
int random_no = 280+(rand()%61); //61=340-280+1 for both 280 & 340; x%61 => 0..60
Well as 340-280=60 and as 32768 (0 to 32767=32768 numbers) divided by 61 gives 537.18 which means that the numbers 49-60 will be created 1/537.18 ~ 0,00186219739292364990689013035382 times less often the the others as 537*61=32.757  (+11 = 32768)

To add comments, please log in or register