MT4: I would like to get one random value from five values defined on global variables

 

For example:

I have defined five global variables as int type, for example:

v1=13, v2=17, v3=23, v4=29, v5=37;


Now after start the robot EA I would like to get one random value from that five values.

Has anobody any idea how to do it ?

 
puncher:

For example:

I have defined five global variables as int type, for example:

v1=13, v2=17, v3=23, v4=29, v5=37;


Now after start the robot EA I would like to get one random value from that five values.

Has anobody any idea how to do it ?


https://docs.mql4.com/math/MathRand
 
get them all and choose internally randomly...
 
puncher:

For example:

I have defined five global variables as int type, for example:

v1=13, v2=17, v3=23, v4=29, v5=37;


Now after start the robot EA I would like to get one random value from that five values.

Has anobody any idea how to do it ?

It is inconvenient to pick "one from five" when each variable has a unique name. From what I can understand you want to pick randomly either v1 or v2 or v3 and so forth based on a random variable. The "trick" is to get a random number ranging between say 1 and 5 and INDEX the variable.

Instead of ...

int v1=13;
int v2=17;
int v3=23;
int v4=29;
int v5=37;

you use an array

int v[]={0,13,17,23,29,37};

Now v[1] is 13

v[2] is 17

and so forth. Note that the array index starts at zero, so I added the "0" as the first element to make it agree with your numbering scheme.

Now you want a random integer ranging from 1 to 5. You could write ...

int index= (MathRand() % 5) + 1;
int randomVariable = v[index]

You will soon get bored indexing from 1 and simplify things by starting from zero!

 
dabbler:

It is inconvenient to pick "one from five" when each variable has a unique name. From what I can understand you want to pick randomly either v1 or v2 or v3 and so forth based on a random variable. The "trick" is to get a random number ranging between say 1 and 5 and INDEX the variable.

Instead of ...

you use an array

Now v[1] is 13

v[2] is 17

and so forth. Note that the array index starts at zero, so I added the "0" as the first element to make it agree with your numbering scheme.

Now you want a random integer ranging from 1 to 5. You could write ...

You will soon get bored indexing from 1 and simplify things by starting from zero!


afaik Global Arrays do not exist.
 
zzuegg:
afaik Global Arrays do not exist.

Well they do. I use them all the time.

Actual code from an EA where these are globals ...

string months[]={"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","xxx","yyy"};
string days[]=  {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
 

you are right, these variables are also global, but i was refering to the https://docs.mql4.com/globals

 
dabbler:

It is inconvenient to pick "one from five" when each variable has a unique name. From what I can understand you want to pick randomly either v1 or v2 or v3 and so forth based on a random variable. The "trick" is to get a random number ranging between say 1 and 5 and INDEX the variable.

Instead of ...

you use an array

Now v[1] is 13

v[2] is 17

and so forth. Note that the array index starts at zero, so I added the "0" as the first element to make it agree with your numbering scheme.

Now you want a random integer ranging from 1 to 5. You could write ...

You will soon get bored indexing from 1 and simplify things by starting from zero!



Thanks a lot...
 
zzuegg:

you are right, these variables are also global, but i was refering to the https://docs.mql4.com/globals


Good point; thanks.

Hopefully the OP was not refering to that type of GLOBAL variable. I was just refering to standard C global variables. I've never used the other sort ... yet :-)

Reason: