Randomness Quality Tester
MathRand() is adequate for cosmetic randomness - which comment string to show, which colour to pick - but it is routinely reached for in simulation contexts (Monte-Carlo position sizing, randomised spread jitter, synthetic tick generation) where its actual statistical properties matter and are usually just assumed rather than checked. This script checks them instead of assuming them. The distribution test draws a configurable number of values from MathRand() and buckets them into equal-width ranges across its documented 0..32767 output, then runs a chi-square goodness-of-fit test against a genuine chi-square critical value table (95% confidence, degrees of freedom 1 through 30) rather than an eyeballed histogram. The same test is run again against a second generator for direct comparison. The period test is a heuristic, not a formal proof: after a fixed reseed it remembers the first several draws as a window, then slides that window forward looking for the first later point where the window repeats exactly. A repeat by pure chance is possible in principle but, for an eight-value window drawn from a 0..32767 range, astronomically unlikely - so a match found this way is a genuine cycle signal, and the script says plainly what it can and cannot prove. The seeding demonstration shows the practical consequence directly: MathSrand(1) followed by five draws, repeated, produces the identical sequence both times, while seeding from GetTickCount() instead produces a different sequence - the concrete reason any program that must not repeat itself run-to-run needs to seed explicitly rather than rely on the compiler's default seed. Finally, the script implements xorshift32, a small, fast generator with a genuinely wide 32-bit output (versus MathRand()'s 15-bit range) and runs it through the same chi-square test, so its distribution quality can be read side by side with MathRand()'s result in the same run. Notes and limitations - The period test is a heuristic window-match, not a formal period proof. A "no repeat found" result means only that no repeat was found within the configured draw cap - the true period could be longer. - The chi-square critical value table only covers 1 to 30 degrees of freedom (InpBucketCount 2 to 31). Larger bucket counts still print the statistic but skip the pass/fail verdict rather than guess. - MathRand()'s actual generator and period are implementation details of this MetaTrader build and are not documented by MetaQuotes as a stable contract - results here describe THIS build, not a guaranteed platform-wide constant. - xorshift32 is a general-purpose, fast, well-distributed generator, not a cryptographic one - it is offered here for simulation work (Monte-Carlo, jitter, synthetic data), not for anything requiring cryptographic unpredictability. - The seeding demonstration's third run (time-based seed) will differ from run to run by design; only the first two fixed-seed runs are expected to match each other.
Script · MT5 · v1.0 · Free
About Randomness Quality Tester
Randomness Quality Tester is a script for MetaTrader 5. It has 5 settings, such as Bucket count, Distribution draws and Period window. MQL source code included.
Files in the download: RandomnessQualityTester.ex5, RandomnessQualityTester.mq5, RandomnessQualityTester.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Bucket count | 10 | Buckets for the chi-square uniformity test (table covers 2..30) |
| Distribution draws | 20000 | Draws used for the distribution test |
| Period window | 8 | Consecutive values compared to detect a repeating cycle |
| Max period draws | 200000 | Give up looking for a repeat after this many draws |
| Xorshift manual seed | 0 | 0 = seed the demo generator from GetTickCount()/TimeLocal() |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Risk & Lot Size · MT5 Script · MQL5 Source Code