Files
mql5-skills/skills/mql5/references/docs/08-math/0611-math-mathrand.md
T
2026-06-23 21:47:51 +08:00

41 lines
1.1 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# MathRand
Returns a pseudorandom integer within the range of 0 to 32767.
```
int  MathRand();
```
Return Value
Integer value within the range of 0 to 32767.
Note
Before the first call of the function, it's necessary to call [MathSrand](/en/docs/math/mathsrand) to set the generator of pseudorandom numbers to the initial state.
Note
Instead of MathRand() you can use rand().
Example:
```
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- set a new initial state to generate a series of pseudo-random integers at each launch
   MathSrand(GetTickCount());
//--- in a loop, display 10 generated pseudo-random integers in the journal
   for(int i=0; i<10; i++)
     {
      int rand_value=MathRand();
      PrintFormat("Pseudorandom integer №%d: %u",i+1,rand_value);
     }
  }
```