MathSrand()

 

An elementary question from a newbie ...

I could not understand the explanation of the reference about the usage of MathSrand(): "The MathRand() function is used for generating a sequence of pseudorandom numbers. Call of MathSrand() with a certain initializing number allows to always produces the same sequence of pseudorandom numbers."

What is the meaning of "... same sequence of pseudorandom numbers" here?

What is the difference between using MathRand() alone and using MathSrand() before MathRand()?

Thanks a lot ...

 
kemalturgay:

An elementary question from a newbie ...

I could not understand the explanation of the reference about the usage of MathSrand(): "The MathRand() function is used for generating a sequence of pseudorandom numbers. Call of MathSrand() with a certain initializing number allows to always produces the same sequence of pseudorandom numbers."

What is the meaning of "... same sequence of pseudorandom numbers" here?

What is the difference between using MathRand() alone and using MathSrand() before MathRand()?

Thanks a lot ...

You need to initially call MathSRand() to start the random number generator with a "seed" value (any value will do, its just a number to start the generator off with).
You then call MathRand() 10 times if you want a sequence of 10 random numbers (or 50 times for a sequence of  50 random numbers ...etc).
If you were to repeat all these steps using the same "seed" value you would end up two sets of random numbers that were exactly the same as each other.
eg If  one particular seed value was used to generate 4 random numbers, it might return, say, 377, 43, 926 & 21 - if you restarted the same random number generator and used the same starting seed value, you would get same four numbers generated, ie 377, 43, 926 & 21 - this is what is meant by "...same sequence of pseudorandom numbers".
If you are writing a new piece of code, you may want to be able to generate a reproducible set of random numbers for debugging purposes, however when your code goes live you would likely want each set of random numbers to be different from earlier runs.
And so MQL has given you the ability to set the "seed" to whatever value you choose.  They recommend, for a live production system, you set the value as "MathSrand(GetTickCount())", since the value of GetTickCount() increases from the moment of the start of the operating system and is not repeated within 49 days, until the built-in counter of milliseconds overflows and starts over again.   Read the documentation again for further information.
 HTH,
John
 
jftjft:
If you are writing a new piece of code, you may want to be able to generate a reproducible set of random numbers for debugging purposes, however when your code goes live you would likely want each set of random numbers to be different from earlier runs.
And so MQL has given you the ability to set the "seed" to whatever value you choose.  They recommend, for a live production system, you set the value as "MathSrand(GetTickCount())", since the value of GetTickCount() increases from the moment of the start of the operating system and is not repeated within 49 days, until the built-in counter of milliseconds overflows and starts over again.   Read the documentation again for further information.

Thanks a lot ... the purpose of MathSrand() function is clear now for me.


If we want to have different sets of random numbers for each run ... is there any difference between a and b below ... that is, can we use MathRand() alone for the same purpose?

a.

MathSrand(GetTickCount());
MathRand();
b.
MathRand();
 
kemalturgay:

Thanks a lot ... the purpose of MathSrand() function is clear now for me.


If we want to have different sets of random numbers for each run ... is there any difference between a and b below ... that is, can we use MathRand() alone for the same purpose?

a.

b.

Before the first call of the function, it's necessary to call MathSrand to set the generator of pseudorandom numbers to the initial state.

(See documentation below) 

MathSrand - MQL4 Documentation
  • docs.mql4.com
MathSrand - MQL4 Documentation
 

Filter, I've used MathRand() alone and it seems to me that it can generate nonrecurring sets of random numbers ... without using MathSrand() before it. Is it a necessary step?

Maybe, there is a general programming concept which I don't know ... as I have just a little bit background on C++ from my old school days.


By the way, if you have time, could you please explain with an example what does "Sets the starting point for generating a series of pseudorandom integers" mean?

What I understand from the above expression is ... when I initialize it with an integer -MathSrand(10) for example- the random numbers captured with MathRand will start with 10. But, not so in reality.


 

Hey mate

I'm not at my trading PC so can't confirm what your seeing with just calling MathRand() without the seed. How it's "supposed" to work is that calling without a seed will always start from some pre-defined "beginning number". That's how it works in all languages I've ever programmed in and  MQL even state this in one of their own articles (link below). I've always just accepted it as standard, good programming practice to always pre-seed a random number generator.

The benefit of seeding is that you can either get a repeatable list of pseudo random numbers or a list of pseudo pseudo randomness by seeding with a different value each time. And seeding with 10 does not mean it will start from 10, that just represents the "beginning number" of the generator. I believe a lot of the help documentation is translated from Russian and sometimes things get lost in translation ;)

Anyway, have a read of this article mate, there's some good info in there :) https://www.mql5.com/en/articles/1496

Cheers
Filter 

MQL4 Language for Newbies. Technical Indicators and Built-In Functions - MQL4 Articles
  • www.mql5.com
MQL4 Language for Newbies. Technical Indicators and Built-In Functions - MQL4 Articles: features of automated forex trading and strategy tester
 
kemalturgay:

Thanks a lot ... the purpose of MathSrand() function is clear now for me.


If we want to have different sets of random numbers for each run ... is there any difference between a and b below ... that is, can we use MathRand() alone for the same purpose?

a.

b.

I believe that b defaults to a. Though I did not compare it directly, often two indicators using any of a or b syntaxes receive the same random number. 

I use workaround of waiting in a loop until the GetTickCount() changes its value.

 
Thanks a lot for all your helps.
Reason: