Download MetaTrader 5

Shuffle array or random number

To add comments, please log in or register
Track Market top sellers!
Louis Stoltz
636
Louis Stoltz 2014.01.07 11:10 

Hi

How can I get a random number between two numbers ie. 1 and 100 with mql4 functions?

And how do you shuffle an array with mql4 with random seed.

Thanks

Simon Gniadkowski
Moderator
17999
Simon Gniadkowski 2014.01.07 11:19  
bonechair:

Hi

How can I get a random number between two numbers ie. 1 and 100 with mql4 functions?

Use MathRand() and scale it to your requirements . . . "The MathRand function returns a pseudorandom integer within the range of 0 to 32767."

int RandomNumber;

RandomNumber = (MathRand() + 327.67) / 327.67;

. . or similar.

Louis Stoltz
636
Louis Stoltz 2014.01.07 15:11  
Thanks alot
whroeder1
14480
whroeder1 2014.01.07 15:16  
bonechair:
How can I get a random number between two numbers ie. 1 and 100 with mql4 functions?
And how do you shuffle an array with mql4 with random seed.
Not compiled, not tested.
// http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
void  ShuffleI(int& values[], int nValues=WHOLE_ARRAY, int ibeg=0){
    if(nValues == WHOLE_ARRAY) nValues = ArraySize(values) - iBeg;
   int iEnd = iBeg + nValues - 1;
   for(int iValue = iEnd; iValue >= iBeg; iValue--){
      int iRand = Random(iBeg, iEnd);
      SwapI(values, iRand, iValue);
   }
}
void  SwapI(int& v[], int a, int b){ int tmp = v[a]; v[a] = v[b]; v[b] = tmp; }
int   Random(int min, int max){ return(MathRand() / 32768.0*(max-min+1)+min); }
Not compiled, not tested.
whroeder1
14480
whroeder1 2014.01.07 15:20  
RaptorUK:

Use MathRand() and scale it to your requirements . . . "The MathRand function returns a pseudorandom integer within the range of 0 to 32767."

RandomNumber = (MathRand() + 327.67) / 327.67;

This produces a random number between [1 .. 100] (inclusive) with equal probabilities, EXCEPT the odds of 100 are 1/32767 NOT 1/100

int   Random(int min, int max){ return(MathRand() / 32768.0*(max-min+1)+min); }

MathRand()/32768.0 produces a random number between [0 .. 1) (not including 1.000000) Scaling by max-min+1 makes it between[min .. max+1) (again not including max+1) therefor the probabilities of max are the same as every other number in the range.

Louis Stoltz
636
Louis Stoltz 2014.01.15 17:17  

Thanks so much.

Do you have an example of what goes into

ShuffleI(int& values[], int nValues=WHOLE_ARRAY, int ibeg=0)

whroeder1
14480
whroeder1 2014.01.15 17:37  
When in doubt, THINK
int array[] = {1,2,3,4,5};
ShuttleI(array);         // Array= {?,?,?,?,?}

int array2[] = {1,2,3,4,5};
ShuttleI(array2,3,1);   // Array2= {1,?,?,?,5}
Louis Stoltz
636
Louis Stoltz 2014.01.15 19:17  
oh ok thanks alot.
Louis Stoltz
636
Louis Stoltz 2014.01.15 19:40  

Ok so can I shuffle an array with a seed number to get an unique array. That is what Im aiming for. For a shuffle function where you use a number, ie. Shuffle(array,seed)

Shuffle(array, 1) = 12345

shuffle(array, 2) = 13254

Do you know how I could accomplish this?

Would this work:

void  ShuffleI(int& values[], int nValues=WHOLE_ARRAY, int ibeg=0, int seed=1){
    if(nValues == WHOLE_ARRAY) nValues = ArraySize(values) - iBeg;
   int iEnd = iBeg + nValues - 1;
   for(int iValue = iEnd; iValue >= iBeg; iValue--){
      int iRand = Random(iBeg, iEnd, seed);
      SwapI(values, iRand, iValue);
   }
}
void  SwapI(int& v[], int a, int b){ int tmp = v[a]; v[a] = v[b]; v[b] = tmp; }
int   Random(int min, int max, int seed){ MathSrand(seed); return(MathRand() / 32768.0*(max-min+1)+min); }
Louis Stoltz
636
Louis Stoltz 2014.01.15 19:51  

I tried doing this with a seed but get the same numbers with whatever seed Im using. It gives me same combination with any number.

void  ShuffleI(int& values[], int seed=1, int nValues=WHOLE_ARRAY, int iBeg=0){
   if(nValues == WHOLE_ARRAY) nValues = ArraySize(values) - iBeg;
   int iEnd = iBeg + nValues - 1;
   for(int iValue = iEnd; iValue >= iBeg; iValue--){
      int iRand = Random(iBeg, iEnd, seed);
      SwapI(values, iRand, iValue);
   }
}
void  SwapI(int& v[], int a, int b){ int tmp = v[a]; v[a] = v[b]; v[b] = tmp; }
int   Random(int min, int max, int seed){ MathSrand(seed); return(MathRand() / 32768.0*(max-min+1)+min); }
whroeder1
14480
whroeder1 2014.01.15 19:54  
bonechair: Would this work:

No it would not. You call Srand ONCE to start a new sequence of MathRand numbers.

If you don't want the same sequence each time use init(){ MathSrand( TimeLocal() ); } like the documentation said.

12
To add comments, please log in or register