Fan sayfamıza katılın
Öyleyse bir link gönderin -
başkalarının da faydalanmasını sağlayın
- Görüntülemeler:
- 11108
- Derecelendirme:
- Yayınlandı:
- Güncellendi:
-
Bu koda dayalı bir robota veya göstergeye mi ihtiyacınız var? Freelance üzerinden sipariş edin Freelance'e git
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 https://prng.di.unimi.it/
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 https://prng.di.unimi.it/xoshiro256starstar.c reference source code by David Blackman and Sebastiano Vigna.
For MetaTrader programmers and traders, this is highly relevant for:
-
Monte Carlo Simulations: Accurately simulating price movements, option pricing, or other stochastic processes.
-
Backtesting and Strategy Optimization: Generating random data or events to test the robustness of a trading strategy under various market conditions.
-
Indicator and Expert Advisor (EA) Development: Providing a reliable source of randomness for EAs that use randomized logic, such as in genetic algorithms for optimization.
-
Research and Analysis: The RandomDoubleHighRes method is particularly useful for researchers and quantitative analysts who require a higher degree of precision for their simulations and models, ensuring more accurate and reliable results than standard MathRand().
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); // Auto-seeded constructor ~Xoshiro256(void) { } // Setting the internal state for the generator void Seed(ulong seed); void Seed(ulong a,ulong b,ulong c,ulong d); // Public methods for generating random numbers int RandomInteger(void); // Random integer [0, INT_MAX] 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, LONG_MAX] 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) double RandomDoubleHighRes(void); // Random double [0.0, 1.0) double RandomDoubleHighRes(const double max); // Random double [0.0, max) double RandomDoubleHighRes(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 with mean 0, std dev 1) double RandomNormal(const double mu,const double sigma); // Random double (normal distribution) // Sampling utilities template<typename T> void Shuffle(T &arr[]); // Shuffle array arr[] in place using Fisher-Yates template<typename T> bool Sample(const T &arr[], T &dest[], int k); // Sample k unique items from arr[] without replacement template<typename T> bool SampleWithReplacement(const T &arr[], T &dest[], int k); };
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 */
Updates:
2023.01.06 - v.1.01 :
- Updated the class to use the more robust 64-bits xoshiro256** random number generator, instead of the 32-bits xoshiro128**.
2025.08.01 - v.1.02 :
- Using a SplitMix64 generator to seed the state to avoid correlation on similar seeds, as recommended by the original authors.
- Added a seedable constructor with a single ulong. The four 64-bit state variables would be generated using SplitMix64 function.
- Increased performance of all the public methods for generating random numbers.
2025.08.03 - v.1.03 :
- Added RandomDoubleHighRes, a new method for generating high-resolution random doubles in the range of [0.0, 1.0). This method is designed to be more precise than RandomDouble, as it can generate all possible double-precision floating-point values within the specified interval, a feature important for scientific simulations and other high-precision applications.
- Corrected potential infinite loop bugs in RandomDouble and RandomDoubleHighRes methods.
- Corrected the implementation of RandomBoolean.
- Faster generation of unbiased random integers in a range by using Lemire's fastrange method. This technique replaces the slow modulo reduction with a debiased integer multiplication.
2025.08.28 - v.1.05
- Improved RNG seeding mechanism to enforce strong bit-avalanche.
- Ensures even closely related seeds (e.g., 0 vs. 1) generate different sequences.
- Prevents correlations between runs and overlap across concurrent RNG instances
2025.09.13 - v.1.06
- Allow richer initialization by using full entropy from all four 64-bit seeds independently.
- Implemented SHA-256 based seeding (uses CryptEncode with CRYPT_HASH_SHA256) to preserve full 256-bit entropy,
2025.09.19 - v.1.08
- Added sampling utilities (Shuffle, Sample, SampleWithReplacement) to the Xoshiro256 class.
2025.10.02 - v.1.09
- Implemented a dedicated boundedUInt32 function using the 32-bit variant of Lemire's fastrange to improve performance.
A BITMAP generated from xoshiro256** Random Number Generator

The code is also mirrored on Algo Forge here: https://forge.mql5.io/amrali/Xoshiro256

RSI Bot, send alert buy when RSI<=20, send alert sell when RSI>=80

Based on the original “Time Segmented Volume (TSV)” developed by Worden Brothers, Inc.

Forex Trade Manager Grid MT5 helps you to managing orders and achieve the goal.

Forex Trade Manager MT5 simplifies managing open orders in MetaTrader 5.