# MathRand( ) Randomness

762

What type of pseudorandom algorithm does the

`MathRand( ) `

uses, and how random is it?

Secondly,is there any way to obtain true random numbers in MQL4 code?

3316

A true random number must be derived from a really random value (e. g. microphone noise), not from a digital formula. The randomness in the MQL4 is similar to the one used in Windows:

For the first:

`   random_number=fractional part of (9821 * r + 0.211327) `

where r = .5 (a seed)

For the next:

`   random_number=fractional part of (9821 * r + 0.211327), `
where r = the previous random number
15719

Proximus:

What type of pseudorandom algorithm does the

uses, and how random is it?

Secondly,is there any way to obtain true random numbers in MQL4 code?

1. MT4 uses the same as most computers.
2. Most RNGs use 32 bits, i.e. returns a double fraction [0..1) MT4 uses something much simpler and return an int [0..32767] and therefor must less random IMO.
3. While there is no true random numbers (computationally generated,) you can make the existing one much better (more statistically independent groups) with a simple function:
Not compiled, not tested.
```#define RANDOM_ARRAY_SIZE 100
#define MATHRANDOM_LIMIT 32768 // Last=32767
int randomArray[RANDOM_ARRAY_SIZE];
void OnInitRandom(int seed=1){
for(int i=0; i < RANDOM_ARRAY_SIZE; i++) randomArray[i] = MathRand();
}
double RandomFraction(){ // Returns double [0..1)
double d = MathRand();
int    i = d * RANDOM_ARRAY_SIZE / MATHRANDOM_LIMIT;
d = randomArray[i] / MATHRANDOM_LIMIT; randomArray[i] = MathRand();
return(d);
}
int   RandomLinear(int max, int min=0){ return(RandomFraction()*(max-min+1)+min); }
// Other probability distributions tbd```
Not compiled, not tested.
762

WHRoeder:
1. MT4 uses the same as most computers.
2. Most RNGs use 32 bits, i.e. returns a double fraction [0..1) MT4 uses something much simpler and return an int [0..32767] and therefor must less random IMO.
3. While there is no true random numbers (computationally generated,) you can make the existing one much better (more statistically independent groups) with a simple function:
Not compiled, not tested. Not compiled, not tested.

Oh thanks, i didnt noticed there was a seeded version of it

`MathSrand( int seed) `

Acutally i think this one is much simpler than the one you wrote in your post:

`MathSrand(Time[0]);`

And more statistically independent since it counts time in seconds and it will not recurr :)

15719

Proximus:

Acutally i think this one is much simpler than the one you wrote in your post:

And more statistically independent since it counts time in seconds and it will not recurr :)

Seeding has nothing to do with my post. MathRand returns a set of numbers. After it returns the total set, which depends on the (9821 * r + 0.211327) the set starts again. Seeding just sets where in the set it begins. If you want to use Srand(Time[0] ) or my OnInitRandom( Time[0] ) you'll get a different set of numbers.

My post was to decrease the correlations of groups of calls per your "true random" question.