Mersenne Twister Random Number Generator
MathRand() has two limitations that matter the moment a program needs more than a rough shuffle, on either platform. Its output is only 15 bits wide (0 to 32767), and its seed is a single value shared by the entire terminal process. Call MathSrand() inside one library - a genetic algorithm, a Monte Carlo permutation test, this header's neighbour projects - and every other piece of code that also calls MathRand() has its sequence reset out from under it, silently, with no error. This header implements the Mersenne Twister (MT19937) algorithm with its own 624-word state array. Seeding it touches nothing else in the program. Its output is full 32-bit, its period is 2^19937-1, and it passes the standard statistical test suites MathRand() was never designed to pass. A fixed seed reproduces the exact same sequence on every run, which is the property that makes two Monte Carlo comparisons or two randomised test fixtures actually comparable. Range and probability draws are also handled correctly rather than the common shortcut. MTNextRange uses rejection sampling, not modulo, because "draw() % span" is measurably biased whenever span does not divide 2^32 evenly - one bucket ends up very slightly more likely than the rest. The demo's bucket-count table exists specifically to make that absence of bias visible. The header auto-seeds from the tick count on first use, so it works without any setup call, exactly like MathRand() does. Call MTSeed() explicitly first in any run that needs to be reproducible. Notes and limitations - Not cryptographically secure. MT19937 is statistically excellent but its output is predictable once enough consecutive values are observed - do not use this for anything security-sensitive. It is intended for simulation, testing and general-purpose randomisation. - Global module-level state, one generator per running program. Two programs running on the same terminal do not share or interfere with each other's state - each compiled program gets its own copy of the included header's variables, on both platforms. - MTSeed() must be called again to reset the sequence; simply calling MTNextUInt32() more times does not return to the start.
Script · MT4 · v1.0 · Free
About Mersenne Twister Random Number Generator
Mersenne Twister Random Number Generator is a script for MetaTrader 4. It has 4 settings, such as Seed, Range min and Range max. MQL source code included.
Files in the download: MersenneTwister.mqh, MersenneTwister.txt, MersenneTwisterDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Seed | 12345 | Fixed seed for the reproducibility check (cast to uint) |
| Range min | 1 | Lower bound for the range/bucket demo |
| Range max | 6 | Upper bound (inclusive) - 6 models a die |
| Draw count | 60000 | Draws used to check the bucket distribution |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code