Xoshiro256** Random Number Generator
xoshiro256** (named after its operations: XOR, shift, rotate) is a pseudorandom number generator designed by Sebastiano Vigna in collaboration with David Blackman
This is a 64-bit all-purpose, rock-solid generator. It has excellent (sub-ns) 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 cryptographic purposes, but is very fast and has excellent statistical properties.
It is presently 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 the
Class Xoshiro256
The class provides an easy interface for random number generation.
//+------------------------------------------------------------------+ //| Class Xoshiro256 | //| Usage: Provides an implementation of the Xoshiro256** algorithm. | //+------------------------------------------------------------------+ class Xoshiro256 { public: //--- Constructor and destructor Xoshiro256(void); // Constructor (auto-seed) Xoshiro256(ulong s0_,ulong s1_,ulong s2_,ulong s3_);// Constructor (custom seed) ~Xoshiro256(void) { } //--- Methods for generation of random numbers int RandomInteger(void); // Random integer [0,2147483648) int RandomInteger(const int max); // Random integer [0,max) int RandomInteger(const int min,const int max); // Random integer [min,max) long RandomLong(void); // Random long [0,ULONG_MAX+1) long RandomLong(const long max); // Random long [0,max) long RandomLong(const long min,const long max); // Random long [min,max) double RandomDouble(void); // Random double [0.0,1.0) double RandomDouble(const double max); // Random double [0.0,max) double RandomDouble(const double min,const double max); // Random double [min,max) bool RandomBoolean(void); // Random true/false (equal probability) bool RandomBoolean(const double prob_true); // Random true/false (with prob_true) double RandomNormal(void); // Random double (normal distribution) double RandomNormal(const double mu,const double sigma); // Random double (normal distribution) };
If your are doing a lot of Monte Carlo simulations, then this xoshiro256** generator will be much faster and more accurate than MQL’s MathRand():
//+------------------------------------------------------------------+ //| speed_benchmark.mq5 | //| Copyright © 2018, Amr Ali | //| https://www.mql5.com/en/users/amrali | //+------------------------------------------------------------------+ #include "Xoshiro256.mqh" //--- the random number generator object Xoshiro256 prng; #define ITER (500*1000*1000) // 500 millions //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnStart() { //--- MathRand() benchmark. uint t1 = GetTickCount(); for(int i = 0; i < ITER; i++) { MathRand(); } uint ticks1 = GetTickCount() - t1; //--- Xoshiro256 benchmark. uint t2 = GetTickCount(); for(int i = 0; i < ITER; i++) { prng.RandomInteger(); } uint ticks2 = GetTickCount() - t2; //--- Display results. PrintFormat("Generating %d random integers: ", ITER); PrintFormat("MathRand() -> %u millisec", ticks1); PrintFormat("Xoshiro256 -> %u millisec", ticks2); PrintFormat("Speed-up factor is %.1fx", (double)ticks1/ticks2); } //+------------------------------------------------------------------+ // sample output: /* Generating 500000000 random integers: MathRand() -> 968 millisec Xoshiro256 -> 422 millisec Speed-up factor is 2.3x */