- www.mql5.com
I tried to make a random number. But it seems that the MathRand() has to program start the same initial value.
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
srand(GetTickCount()); int random_no = 280+(340-280)*rand()/32768;
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
- www.mql5.com
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.
Glad you got it sorted.
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.); }
|
honest_knave:
srand(GetTickCount()); int random_no = 280+(340-280)*rand()/32768; |
|
whroeder1:
- 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.
what about:
srand(GetTickCount()); // in OnInit() ... int random_no = 280+(rand()%61); //61=340-280+1 for both 280 & 340; x%61 => 0..60Well 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)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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