xoshiro256 Random Number Generator - MetaTrader 5 Library | Trading Script Download - MT4/MT5 Resources

xoshiro256** (named after its operations: XOR , transfer , rotate ) is a pseudo-random number generator designed by Sebastiano Vigna in collaboration with David Blackman https://prng.di.unimi.it/
This is a 64-bit universal, rock-solid generator. It has excellent (sub-nanosecond) speed, a state size (256 bits) that is large enough for any parallel application, and it passes all known statistical tests.
The xoshiro256** algorithm is not suitable for encryption purposes, but is very fast and has excellent statistical properties.
It is currently used in Microsoft's .NET Framework and the JavaScript engines of Chrome, Node.js, Firefox, Safari, and Microsoft Edge.
The algorithm used here is translated from https://prng.di.unimi.it/xoshiro256starstar.c with reference to the source code of David Blackman and Sebastiano Vigna.
For MetaTrader programmers and traders, this is highly relevant to:
Monte Carlo simulation : Accurately simulates price movements, option pricing, or other stochastic processes.
Backtesting and strategy optimization : Generate random data or events to test the robustness of trading strategies under various market conditions.
Indicator and Expert Advisor (EA) development : providing a reliable source of randomness for EAs using stochastic logic, such as genetic algorithms for optimization.
Research and Analysis : The RandomDoubleHighRes method is particularly useful for researchers and quantitative analysts who require higher precision simulations and models, ensuring more accurate and reliable results than standard Mathematical RAND ().
This class provides a simple interface for random number generation.
//+------------------------------------------------------------------+ //| Xoshiro256 class | //|Usage: Provide implementation of Xoshiro256** algorithm. | //+------------------------------------------------------------------+ Class Koushiro 256 { people : // Constructor and destructor Xoshiro256( blank ); // Automatic seed constructor ~Xoshiro256( blank ) { } // Set the internal state of the generator to blank seed ( Oolong seed); Blank seeds ( Oolong 1, Oolong B, Oolong C, Oolong d); // Public method to generate random numbers integer Random integer ( blank ); // Random integer [0, INT_MAX] integer Random integer ( constant integer maximum); // Random integer [0, max) integer Random integer ( constant integer minute, constant integer maximum); // Random integer [minimum, maximum) long Random long ( blank ); // Random long [0, LONG_MAX] long Random length (maximum limit of constant length ); // Random length [0, max) long Random length (minutes of constant length, maximum limit of constant length); // Random length [minimum value, maximum value] double random double ( blank ); // Random double precision [0.0, 1.0) double random double (maximum limit of constant double); // Random double precision [0.0, max) double Random double (constant double minutes, constant double max ) ; // Random double [min, max) double random double res ( blank ); // Random double [0.0, 1.0) double random double res ( constant double max); // Random double [0.0, max) double random double res ( constant double minutes, constant double max); // Random double [min, max] boolean Random boolean ( blank ); // Random true/false (equal probability ) boolean Random boolean (constant double probability of true); // Random true/false (using prob_true) double random normal ( blank ); // Random double (normal distribution with mean 0, standard deviation 1) double random normal ( constant double mu, constant double sigma); // Random Double (Normal Distribution) // Sampling utility template < type name > Blank Shuffle(T &arr[]); // Use Fisher-Yates to shuffle the array arr[] into template <<span class="keyword">Type name> Boolean sample ( constant T &arr[], T &dest[], integer k); // Sample k unique items from arr[] without replacement template < type name > Boolean replacement sample ( constant T &arr[], T &dest[], integer k); };
If you are doing a lot of Monte Carlo simulations, this xoshiro256** generator will be faster and more accurate than MQL's MathRand():
//+------------------------------------------------------------------+ //| speed_benchmark.mq5 | //| Copyright © 2018, Eminem Ali | //| https://www.mql5.com/en/users/amrali | //+------------------------------------------------------------------+ #includes "Xoshiro256.mqh" //--- Random number generator object Xoshiro256 prng; #define ITER ( 500 * 1000 * 1000 ) // 500 million //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ blank start () { //--- MathRand() benchmark. Unit t1= Get the number of ticks (); for ( integer i = 0 ; i < ITER; i++) { math rand (); } Unit scale 1 = get number of ticks () - t1; //--- Xoshiro256 benchmark. Unit t2= Get the number of ticks (); for ( integer i = 0 ; i < ITER; i++) { prng.RandomInteger(); } Unit scale 2 = get the number of scales ()-t2; //--- display the result. Print format ( "Generate %d random integers:" , International Thermonuclear Fusion Experimental Reactor); Print format ( "MathRand() -> %u milliseconds" , scale 1); Print format ( "Xoshiro256 -> %u milliseconds" , scale 2); Print format ( "Acceleration coefficient is %.1fx" , ( double ) scale 1/scale 2); } //+------------------------------------------------------------------+ // Sample output: /* Generate 500000000 random integers: MathRand() -> 968 milliseconds Xoshiro256 -> 422 milliseconds acceleration factor is 2.3 times*/
renew:
2023.01.06 - v.1.01:
2025.08.01 - v.1.02:
2025.08.03 - v.1.03:
2025.08.28 - v.1.05
2025.09.13 - v.1.06
2025.09.19 - v.1.08
2025.10.02 - v.1.09
Bitmap generated by xoshiro256** random number generator
The code is also reflected on Algo Forge: https://forge.mql5.io/amrali/Xoshiro256
Attachment download
📎 Xoshiro256.mqh (43.44 KB)
📎 demo_examples.mq5 (7.19 KB)
📎 speed_benchmark.mq5 (3.07 KB)
Source: MQL5 #42098
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •