Choose between two randomly selecting choices

 

How to get the EA to choose between two randomly selecting choices with certain probability. Example: I want EA "to decide" whether or not to open a new order with the following probability: 70% opens a new order against 30% likely not to open a new order?

Of course "decision" is not static, but varibale with probability that changes from bar to bar.

 

maybe

int num = 100*MathRand()/32767; // random number between 0 to 100

if (num <= 30)
   Alert ("Go for The 30 Percent");
else
   Alert ("Go for The 70 Percent");
 

Thanks. However somethimes it does not produce any result, but I need every time one of 2 option to be selected.

 MathSrand(TimeLocal());
  double num = NormalizeDouble((100*MathRand()/32767),0); // random number between 0 to 100
  

if (num <= 20)
   Print ("Go for The 30 Percent");
else
if (num> 80)
   Print ("Go for The 70 Percent");

How can this be rosolved?

 
MINTA:

Thanks. However somethimes it does not produce any result, but I need every time one of 2 option to be selected.

How can this be rosolved?

In your version you will sometimes get a value between 21 and 80 which is skipped because your conditional tests are not correct. The code posted by gjol will always return one or the other results.

Regards, Paul.

 

sir, y u use NormalizeDouble()

what's the difference between

double num = NormalizeDouble((100*MathRand()/32767),0); // random number between 0 to 100

&

int num = 100*MathRand()/32767; // random number between 0 to 100

what do u mean by "somethimes it does not produce any result" ?

& y u use 20

if (num <= 20

u said 30%

BTW

if (num <= 20)
   Print ("Go for The 30 Percent");
else
if (num> 80)
   Print ("Go for The 70 Percent");

is the same has

if (num <= 20)
   Print ("Go for The 30 Percent");
else
   Print ("Go for The 70 Percent");
 
Yes, you are right.
 
  1. qjol:

    maybe not

    int num = 100*MathRand()/32767; // random number between 0 to 100
    

    MathRand returns an int. int/32767 will be zero. Dividing by 32767. will result is equal probability from 0 to 99 and only a 1/327.7 probability of generating 100.

    int num = 100*MathRand()/32768.; // random number from 0 through 99 with equal probability.
    

  2. MINTA:
    probability: 70% opens a new order against 30% likely not to open a new order?
    double fraction = MathRand()/32768.;
    if (fraction < 0.30) ..
Reason: