help needed for random numbers within a range

 
https://docs.mql4.com/math/MathRand and why not use the search function believe me it's free!
 
farhang wrote >>
https://docs.mql4.com/math/MathRandand why not use the search function believe me it's free!

The MathRand function returns larger integers like 32767 and i have to then start using mod to reduce and reduce. not what i want.
thks.
 

Use MathFloor(MathRand(TimeLocal())/3276)

 
Roger:

Use MathFloor(MathRand(TimeLocal())/3276)

MathRand() has no inputs; I guess u meant calling MathSrand(TimeLocal()) before calling MathRand(). Anyway, an alternative way (and arguably a more 'standard' way of using MathRand()):

MathSrand(TimeLocal());
int num = MathRand()%10 + 1;     // random number between 1 to 10 (even distribution)

You can find an excellent explanation in the section about MathRand() and MathSrand() here -> Articles -> Features -> MQL4 Language for Newbies. Technical Indicators and Built-In Functions.
 
No! Never take the least significant bits of a random number, they are not very random. Always use the most significant bits.
int num = 1 + 10*MathRand()/32768; // 1-10
Also note you must divide by one more than MathRand's max value (32767) to get the proper range.
 
WHRoeder:
No! Never take the least significant bits of a random number, they are not very random. Always use the most significant bits.

Interesting! Please explain.

 
Also look at the MathSrand() seeding function and be aware that the sequence of random numbers that you generate will repeat itself unless you change the seed.
And generating a series of unique seeds may cause you a problem when performing repeated backtests on the same data in the tester.

CB
 
I was referring to "Never take the least significant bits of a random number, they are not very random". I studied EE and not computer science, so I might have some theoretical gaps in my knowledge in this regard. Googling this hasn't brought any relevant results. I thought those sort of problems were relevant years ago, but not with modern pseudo-random number systems.
Reason: