Here is a code snippet for Random Seed initialisation from milliseconds or cpu clock counter

 

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:

#import "kernel32.dll"
   void GetLocalTime(int& TimeArray[]);
   bool QueryPerformanceCounter(int& counter[]);
#import

void initRandom() {
   int seed = TimeLocal();
   
   int timeArray[4];
   int millisec;
   GetLocalTime(timeArray);
   millisec = timeArray[3] >> 16;
   if (millisec != 0) {
      seed = millisec;
   }
   
   int counter[2];
   int counterSeed;
   if (QueryPerformanceCounter(counter)) {
      counterSeed = (counter[0]);
      if (counterSeed != 0) {
         seed = counterSeed;
      }
   }
   
   MathSrand(seed);
   return;
}
 
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
Files:
 

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);
}
//+------------------------------------------------------------------+
Reason: