mathSrand 2 EA same time

 

For the love of God, can someone help me? xD

i've being cracking my head out and can't figure it out.


I have an EA, runing in 6 pairs (each running an instace of the EA), and i need a different rand value for each pair runing, each time functions run.


code is simple as 

MathSrand(TimeLocal());
int num = MathRand()%10000 + 10000;
Print(num);

Each time i compile code, all pairs print the same number...

Files:
 

I suppose you're calling the MathSrand on the OnInit function. Considering you said the numbers are always the same when you recompile the code, that happens because the EAs attached to a chart are reloaded. You're using TimeLocal as a seed, and this function counts the seconds from the epoch (jan/01 1970). Look at your screenshot: all messages are printed at the same second. This means their seeds are the same, so they'll always print the same number when the LocalTime is called in the exact same second.


You need something that is divergent on each EA to add to your seed - for example, you could take the current time (GetTickCount64 is preferrable when working with random number generators seeds) and add the EA magic number to it. Every seed would be different most of the time. Not everytime, though. Imagine a situation where the second count used as a seed is 1000 and the magic number is 3. Also imagine another EA is reloaded at the second 1001, but its magic number is 2. If you simply add the magic to the current second count, both would still have the same seed. You need to create some more complex math for this. Another more simple option would be to manage a global variable (GlobalVariableSet) to store every next number that would be added to the seed.

 
matheus:

For the love of God, can someone help me? xD

i've being cracking my head out and can't figure it out.


I have an EA, runing in 6 pairs (each running an instace of the EA), and i need a different rand value for each pair runing, each time functions run.


code is simple as 

Each time i compile code, all pairs print the same number...

Try :

MathSrand(GetTickCount());
 
Emanuel Cavalcante Amorim Filho #:

I suppose you're calling the MathSrand on the OnInit function. Considering you said the numbers are always the same when you recompile the code, that happens because the EAs attached to a chart are reloaded. You're using TimeLocal as a seed, and this function counts the seconds from the epoch (jan/01 1970). Look at your screenshot: all messages are printed at the same second. This means their seeds are the same, so they'll always print the same number when the LocalTime is called in the exact same second.


You need something that is divergent on each EA to add to your seed - for example, you could take the current time (GetTickCount64 is preferrable when working with random number generators seeds) and add the EA magic number to it. Every seed would be different most of the time. Not everytime, though. Imagine a situation where the second count used as a seed is 1000 and the magic number is 3. Also imagine another EA is reloaded at the second 1001, but its magic number is 2. If you simply add the magic to the current second count, both would still have the same seed. You need to create some more complex math for this. Another more simple option would be to manage a global variable (GlobalVariableSet) to store every next number that would be added to the seed.

GetTickCount64  would not work as MathSRand() needs an uint.

 
Alain Verleyen #:

GetTickCount64  would not work as MathSRand() needs an uint.

True, thank you for pointing that out.
 

I get it actually, and i already used my magic number, kind in the same way you described, but, the thing is, i'm trying to send a message to telgram, so, to prevent flood protection i need that function to have a sleep time between 2000 to 8000 milliseconds throughout each pair, each time it runs.

The function is calculated every new candle (by candle time, each pair), so all of them will be the same seed. 


I dont know if i'm missing something, i just cant figure it out how to make  a function that will alway have a rand number between 2000 and 8000 and is diferent to every ea running, each time function runs in each pair.

I already found a way to "bypass" this issue, but, sometimes, user could insert a number too high in magicNumber, making EA unusable.


If you have any ideas, i'd really appreciate.

Also, sorry for my bad english or any mistakes. Not my native language.

[...]

if(novocandle_D1())
 {
  msg = "message to be sent";
  notificacao(msg);
 }

[...]

void notificacao(string msg)
  {
   if(notificarTelegram)
     {
      MathSrand(TimeLocal());
      int num = rand();
      Print(num);
      Sleep(MagicNum*1000);
      
      int res = bot.SendMessage(ChatID, msg);
      if(res != 0)
         Print("Error: ", GetErrorDescription(res));
     }
   if(notificarCelular)
      SendNotification(msg);
  }
i just saw you're Brazilian, me too, if you feel warmth in your heart helping me, we could speak in portuguese in Telegram xD
 
Use MathRand() directly without calling MathSrand() first. You will get a different sequence each time you start the EA.

 
amrali #:
Use MathRand() directly without calling MathSrand() first. You will get a different sequence each time you start the EA.

I see, but, number cant be more than 8000. 

But then, i should be able to make a function to validate the number until number is below 8000. I'll try that, thank you very much :D

 
matheus: Each time i compile code, all pairs print the same number...

Of course, they do. You compiled, all EAs start, all use the same timestamp (one-second window), all sequences start identically. Stop using sRand.

 
Another solution when you start the same EA on multiple charts is using 

MathSrand((int) ChartID))
Reason: