# Shuffle array or random number

636

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

Moderator
18018

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.

636

Thanks alot
15085

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.
15085

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.

636

Thanks so much.

Do you have an example of what goes into

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

15085

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}```
636

oh ok thanks alot.
636

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); }```
636

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); }```
15085

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