void initRandom() { MathSrand( GetTickCount() ); return; }
Hi WHRoeder,
Yes, it can replace getting millisec part, you have right.
But because GetTickCount() is only millisecond resolution, does not solve the initial problem.
Try the follwing script, and you get something like this:
2012.04.02 15:42:25 TestGetTickCount EURUSD,M5: -1775969938
2012.04.02 15:42:25 TestGetTickCount EURUSD,M5: 448221765
2012.04.02 15:42:25 TestGetTickCount EURUSD,M5: -1775970107
2012.04.02 15:42:25 TestGetTickCount EURUSD,M5: 448221765
2012.04.02 15:42:25 TestGetTickCount EURUSD,M5: -1775970174
2012.04.02 15:42:25 TestGetTickCount EURUSD,M5: 448221765
Yes, it can replace getting millisec part, you have right.
But because GetTickCount() is only millisecond resolution, does not solve the initial problem.
Try the follwing script, and you get something like this:
2012.04.02 15:42:25 TestGetTickCount EURUSD,M5: -1775969938
2012.04.02 15:42:25 TestGetTickCount EURUSD,M5: 448221765
2012.04.02 15:42:25 TestGetTickCount EURUSD,M5: -1775970107
2012.04.02 15:42:25 TestGetTickCount EURUSD,M5: 448221765
2012.04.02 15:42:25 TestGetTickCount EURUSD,M5: -1775970174
2012.04.02 15:42:25 TestGetTickCount EURUSD,M5: 448221765
Files:
testgettickcount.mq4
1 kb
First of all, thank you very much for sharing your work. Let me share how i use it.
Here is an other (script) version which makes you able to get a random integer number within a pre-defined range with "step" parameter (like the optimization works in the strategy tester):
//+------------------------------------------------------------------+ //| RandomScript.mq4 | //+------------------------------------------------------------------+ #property show_inputs #import "kernel32.dll" bool QueryPerformanceCounter(int& counter[]); #import extern int iterations = 100; extern int start = -1000; extern int step = 5; extern int stop = 1000; //+------------------------------------------------------------------+ //| script start function | //+------------------------------------------------------------------+ int start() { for(int i=0; i<=iterations; i++) { Comment("\n"+" iteration = "+i+"\n"+ " start = "+start+"\n"+ " step = "+step+"\n"+ " stop = "+stop+"\n"+ " result = "+rand(start,step,stop)); Sleep(rand(500,5,750)); } Comment(""); return(0); } //+------------------------------------------------------------------+ int rand(int start, int step, int stop) { int seed; int counter[2]; int difference = stop-start; if(difference%step!=0) { Print("wrong values!"); return(0); } if (QueryPerformanceCounter(counter)) { seed = MathAbs(counter[0]%(difference+1)); } int rnd = MathRound(seed/step)*step; int res = start+rnd; return(res); } //+------------------------------------------------------------------+

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi All,
I found that at terminal starting, all Random calls from different places starts with the same sequence of pseudo-random numbers. Even after MathSrand(TimeLocal()) call, because TimeLocal() gives the same second at terminal start. Here is a code snippet, that solves this problem, I thought share it: