How does MathRand() generate values?

 

Please tell me how MathRand() gets values.

Can we expect MathRand() to be evenly distributed within the stated range?

 

It has been discussed many times on the four forum. A search for the word MathRand yields more than 15 pages of results

For example, these topics:

https://www.mql5.com/ru/forum/123337

https://www.mql5.com/ru/forum/111855

https://www.mql5.com/ru/forum/111974

https://www.mql5.com/ru/forum/125667

Генерация равномерно распределенных случайных чисел (0,1) - MQL4 форум
  • www.mql5.com
Генерация равномерно распределенных случайных чисел (0,1) - MQL4 форум
 
yu-sha:

Please tell me how MathRand() gets values.

Can we expect MathRand() to be evenly distributed within the stated range?

In my opinion, you can do a distribution test, but I don't remember how to do it.

I checked it in php some time ago.

It seems to generate large number of random numbers, and then plot.

Or you can find number of equal numbers for rough estimation, for example.

Generate a couple hundred numbers and write them to a file, then graph them with at least Excel.

 

stringo,

I have not been able to read all the links offered to read))

Looking at the number of posts and the composition of participants in the discussion, I made a conclusion:

- You can safely use MathRand()

- If you don't have enough "resolution" 0...32767, you may use MathRand()*MathRand() - the distribution will not become strongly "skewed" in one or another side

mrProF,

It is not difficult to perform a rough estimation.

int A[32768] ;

for (int i=0; i<100000000; i++)

A[MathRand()]++;

but why?

For my purposes, the developers' comment is enough

 
yu-sha:

For my purposes, the developers' comment is enough

The developers didn't develop MathRand themselves, they just implemented translation of calls directly to CRT functions srand and rand.

It's good for general use.

 
And if you want "wow," then https://www.mql5.com/ru/forum/123337/page16 the lowest post.
Генерация равномерно распределенных случайных чисел (0,1) - MQL4 форум
  • www.mql5.com
Генерация равномерно распределенных случайных чисел (0,1) - MQL4 форум
 
gumgum:
And if you want to "wow" then https://www.mql5.com/ru/forum/123337/page16 the lowest post.

I need to randomly shift weights in fuzzy logic algorithms

The main thing is not to have constant "blockages" in any one direction.

Thanks for the "wow", of course, but in my case it's unnecessary))

 

on the account of

"- if there is not enough "resolution" 0...32767, you can take MathRand()*MathRand(), - the distribution will not become very "skewed" to one side or another"

I was wrong ...

expectation will shift to the left from the middle of the range (I suspect that to 1/4 * 32768^2, but I'm not sure of anything)

Anyway, my statement was wrong

 
yu-sha:

on the account of

"- if there is not enough "resolution" 0...32767, you can take MathRand()*MathRand(), - the distribution will not become very "skewed" to one side or another"

I was wrong ...

expectation will shift to the left from the middle of the range (I suspect that to 1/4 * 32768^2, but I'm not sure of anything)

in general, my statement was wrong

hmlo d-range [0, (2^rstep)-1]

long ranD(int rstep)
{
long div=1;
long r=0; 
   for(int i=1;i<=rstep;i++)
   {
   if(MathRand()+1>16383.5){if(i==1){r=1;}else{r+=div;}}
   div=div*2;
   }
return(r);
}

You can use int or double or ulong instead of long.

 

gumgum,

rstep [0, 2^rstep-1] - did not understand what this parameter is

Question:

The double variable needs to be changed randomly with an accuracy of, say, up to the 8th decimal place

MathRand()/32768 will give a step of 0.00003 - not enough.

How to get uniformly distributed on (0...1) random value with step 0.00000001 with MathRand() ?

 
yu-sha:

gumgum,

rstep [0, 2^rstep-1] - did not understand what this parameter is

Question:

The double variable needs to be changed randomly with an accuracy of, say, up to the 8th decimal place

MathRand()/32768 will give a step of 0.00003 - not enough.

How do I get a random variable evenly distributed at (0...1) with a sampling rate of 0.00000001 using MathRand() ?


at rsign=1 (-1,1) at rsign=0 (0,1)

double ranD(int rsign,int rstep)
{
double div=2;
double r=0; 
   for(int i=1;i<=rstep;i++)
   {
   if(MathRand()+1>16383.5){r+=1/div;}
   div=div*2;   
   }
      if(rsign==1)
         {
         r=2*r-1;
         }
return(r);
}

rstep is a degree