Random Variate Factory
MQL4 ships MathRandom(), a 0..32767 uniform draw, and nothing built on top of it. Modelling slippage spikes, gap risk, or the size of extreme drawdowns realistically needs a distribution with a heavier or more skewed tail than the plain normal or uniform - the Laplace (double exponential), Pareto, and generalised Pareto distributions are the standard choices for exactly that kind of tail behaviour, and none of them are one line of MathRandom arithmetic to sample correctly. Every sampler here uses the inverse transform method: draw a uniform U on the open interval (0,1), then apply that distribution's quantile function (the mathematical inverse of its CDF) to U. This is the textbook EXACT method wherever a closed-form quantile function exists, which is true for all three distributions offered - no rejection sampling, no approximation, no bias from a truncated series. A single MathRandom() call only carries 15 bits of resolution, which is too coarse once a quantile function has a steep tail - Pareto's xm/U^(1/alpha) blows up fast as U approaches zero, and a coarse U produces visibly clustered, repeated draws in the tail. UniformOpen01 combines two MathRandom() calls into roughly 30 bits of resolution instead of relying on one raw draw, and is also careful to never return exactly 0.0 or 1.0 - several of the quantile functions below divide by U or take log(1-U) and would otherwise produce +-infinity at either endpoint. Notes and limitations - SamplePareto and SampleGeneralizedPareto with a positive shape parameter are genuinely heavy-tailed by design: Pareto with alpha<=1 has no finite mean, and alpha<=2 has no finite variance. Neither is rejected as an invalid input - both are legitimate parameter choices for deliberately fat-tailed modelling, and the sampler still draws correctly from them even though a sample mean over a finite run will not settle down for alpha<=1. - SampleGeneralizedPareto with a negative shape parameter produces a BOUNDED distribution with a hard upper limit at mu - sigma/xi - draws never exceed that value, which the demo verifies directly over 2000 samples. - UniformOpen01's roughly 30-bit resolution is a large improvement over a single MathRandom() call but is still a linear congruential generator under the hood (MQL4's MathRandom), not a cryptographic or even a particularly long-period generator - fine for Monte Carlo style simulation and stress testing, not suitable for anything requiring cryptographic randomness.
Script · MT4 · v1.0 · Free
About Random Variate Factory
Random Variate Factory is a script for MetaTrader 4. It has 2 settings, such as Seed and Sample size. MQL source code included.
Files in the download: RandomVariateFactory.mqh, RandomVariateFactory.txt, RandomVariateFactoryDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Seed | 42 | MathSrand seed, fixed so the demo prints the same numbers every run |
| Sample size | 2000 | how many draws per distribution when checking the sample mean |
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