Xoshiro256/demo_examples.mq5

102 lines
7.2 KiB
MQL5
Raw Permalink Normal View History

2025-09-19 07:21:01 +03:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| demo_examples.mq5 |
//| Copyright <EFBFBD> 2025, Amr Ali |
//| https://www.mql5.com/en/users/amrali |
//+------------------------------------------------------------------+
#include "Xoshiro256.mqh"
void OnStart()
{
//--- the random number generator object
Xoshiro256 prng;
//--- test RandomInteger() function
int arrInt[20];
Print("Random integers in the range [5,100): ");
for(int i=0; i<ArraySize(arrInt); i++)
arrInt[i]=prng.RandomInteger(5,100);
ArrayPrint(arrInt);
//--- test RandomDouble() function
double arrDbl[10];
Print("Random prices in the range [0.7,2): ");
for(int i=0; i<ArraySize(arrDbl); i++)
arrDbl[i]=prng.RandomDouble(0.7,2);
ArrayPrint(arrDbl,_Digits);
//--- Generate and print some random dates - RandomLong()
Print("Random dates from 1990.01.01 till today: ");
for(int i=0; i<7; i++)
{
datetime dt=(datetime)prng.RandomLong(D'1990.01.01',TimeCurrent());
Print(dt);
}
//--- Print a random password whose length is 16
string str_pass="";
string alphabet="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for(int i=0; i<16; i++)
{
int rand_pos=prng.RandomInteger(StringLen(alphabet));
str_pass+=StringSubstr(alphabet,rand_pos,1);
}
Print("Random password: ",str_pass);
//--- Histogram for 64-bits frequency distribution.
int hist[64] = {0};
int sample=1e7;
for(int i=0; i<sample; i++)
{
long num = prng.RandomLong(LONG_MIN,LONG_MAX); // num = MathRand();
for(int bit=0; bit<=63; bit++)
hist[bit] += (int)(((ulong)num >> bit) & 1);
}
Print("The 64-bits frequency distribution histogram:");
ArrayPrint(hist);
// --- Test Sample (without replacement) ---
int arr[] = {1,2,3,4,5,6,7,8,9};
int sample1[];
prng.Sample(arr, sample1, 4);
Print("Sample (no replacement, k=4): ");
ArrayPrint(sample1);
// --- Test SampleWithReplacement ---
int sample2[];
prng.SampleWithReplacement(arr, sample2, 6);
Print("SampleWithReplacement (k=6): ");
ArrayPrint(sample2);
// --- Test Shuffle ---
prng.Shuffle(arr);
Print("Shuffled: ");
ArrayPrint(arr);
}
//+------------------------------------------------------------------+
/* sample output:
Random integers in the range [5,100):
16 75 5 41 93 13 83 73 23 94 76 37 34 45 9 57 29 86 30 56
Random prices in the range [0.7,2):
1.97978 1.65321 1.31701 1.63440 1.81798 1.10520 1.47172 0.90531 1.59575 0.87328
Random dates from 1990.01.01 till today:
1999.11.11 18:31:15
2014.12.18 01:10:19
2012.07.31 12:48:45
2022.05.22 18:40:11
2017.01.28 05:07:25
1996.08.03 05:02:06
1990.07.08 07:04:22
Random password: nlpa3yekQFRGvIU4
The 64-bits frequency distribution histogram:
[ 0] 4998374 5000560 5000055 4997115 4998424 4996988 4999278 5000736 4998032 4998934 5000479 4999571 5001382 5000935 4999165 5002358
[16] 4996734 4998310 5003839 4999034 4999299 4998484 4999099 5000110 5000423 5000866 4999718 4998784 5001812 4999260 4996949 5002480
[32] 5001132 5001157 5002466 4998330 4998042 4997632 5000654 5000393 5002430 4998963 5000510 5000215 4998870 5002737 4999696 5000776
[48] 4999807 5001265 4997772 4999233 5000133 4997519 4999916 4999233 4997916 4999944 4998269 4999548 4998959 4999644 5001790 5003097
Sample (no replacement, k=4):
9 1 8 4
SampleWithReplacement (k=6):
7 4 4 8 5 5
Shuffled:
4 5 9 3 2 6 8 7 1
*/