
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Please tell me how MathRand() gets values.
Can we expect uniform distribution of MathRand() in declared range?
here is one example, of course there are other, more complex algorithms...
source rand.c :
A RNG function is required with the number Int from 0 to any value.
Since the maximum value of Int =2,147,483,647, we calculate up to it.
We have obtained this function. I think the distribution is uniform.
The article has a generator up to 4294967295
Alglib SB has a high precision oscillator
UPD: I tried ithttps://www.mql5.com/ru/forum/324066#comment_13500222 , it seems to work, but there is no documentation for Alglib in MQL5, you should read it on the Alglib website
The article has a generator to 4294967295
Thanks, I compared it in terms of computational speed.

My function on standard rand() turned out to be 2 times slower. Although the code seemed much simpler...
There is a high precision generator in Alglib
UPD: I tried ithttps://www.mql5.com/ru/forum/324066#comment_13500222 , it seems to work, but there is no documentation for Alglib in MQL5, you should read it on the Alglib website
I saw it but I want it without alglib. In addition, that function has a lot of */%. I have not compared its speed since it is obviously slower. The generator from the article works with bit shifts - it's faster.
Rewrote my function to optimise the speed:
The distribution turned out to be uniform. See images of the experiment in the blog https://www.mql5.com/ru/blogs/post/735953I rewrote the RNG from the article.
I discarded unnecessary things, and this is what I got:
Distribution turned out to be equal too.
I compared speed of both functions, original from article and simple MathRand():
Shortened according to the article rnu.Rand_01() - 596 ms
My optimized version of RandomInteger() - 840 ms (old version 1200 ms, i.e. 25% faster)
Just by MathRand() - 353 ms (fastest, but distribution will be uneven. If the range of numbers required is greater than 32767, the result will be skipped. If less than 32767, for example i=31111. then rounding will be more frequent to some points).
Take advantage)
Why complicate things so much?
You can do it this way (roughly speaking):
And if you want to play around or see what it shows, you can do this:
and see how it changes values.
I have rewritten my function to optimize speed:
The distribution is even. See images of the experiment on the blog https://www.mql5.com/ru/blogs/post/735953I rewrote the RNG from the article.
I discarded unnecessary things and that's what I got:
Distribution turns out to be uniform too.
I compared it with the distribution from the help, I don't see any difference