Random number only returns one set of results

To add comments, please log in or register
prweza
426
prweza  
I have this code. I want TP to print either 500  or 1000 depending on  the random numbers generated, the problem is only  TP 1000 seems to be printed, I'm sure there is an easy way around this but cant figure it out. Please hel
int BAH= rand()%5+1;

 int TakeProfit2=0;
  bool BAH2 = (1||2||3);
  bool BAH1 = (4||5);
  
  if( BAH2){
   {
   TakeProfit2 =500;
   }
   
   if( BAH1)
    {
   TakeProfit2 =1000;
    
   }
   
   }
  
    Print("take profit",TakeProfit2 );

p
Fernando Carreiro
4419
Fernando Carreiro  
prweza: I have this code. I want TP to print either 500  or 1000 depending on  the random numbers generated, the problem is only  TP 1000 seems to be printed, I'm sure there is an easy way around this but cant figure it out. Please help

Your code does not make much sense with respect to the Boolean expression you are using, because:

bool BAH2 = (1||2||3);  // will always be 'true', because any non-zero value is equivalent to 'true', so (1||2||3) is (true||true||true) = true
bool BAH1 = (4||5);     // will always be 'true', because any non-zero value is equivalent to 'true', so (4||5) is (true||true) = true

Also, both Boolean variables are constant and don't depend in any way on the BAH variable. Maybe what you are trying to achieve is the following:

bool BAH2 = (BAH >= 1) && (BAH <= 3); // or (BAH == 1) || (BAH == 2) || (BAH == 3)
bool BAH1 = (BAH >= 4) && (BAH <= 5); // or (BAH == 4) || (BAH == 5)
Anthony Garot
1862
Anthony Garot  
prweza:
I have this code.

Don't forget MathSrand().

Per the documentation:

"Before the first call of the function, it's necessary to call MathSrand to set the generator of pseudorandom numbers to the initial state."

prweza
426
prweza  
Fernando Carreiro:

Your code does not make much sense with respect to the Boolean expression you are using, because:

Also, both Boolean variables are constant and don't depend in any way on the BAH variable. Maybe what you are trying to achieve is the following:

Ok thanks, if i want to make 1 to 3  equals to a constant called Takeprofit= 5000

and 4 to 5 equal to a take profit 1000 how do i do that ?  My approach above seems to return 1 variable only.

Fernando Carreiro
4419
Fernando Carreiro  
prweza: Ok thanks, if i want to make 1 to 3  equals to a constant called Takeprofit= 5000 and 4 to 5 equal to a take profit 1000 how do i do that ?  My approach above seems to return 1 variable only.

Maybe I am misunderstanding your new question, but did I not just reply to that?

Maybe I should show the complete code for easier understanding:

srand( GetTickCount() );

int
   BAH         = rand() % 5 + 1,
   TakeProfit2 = 0;

bool
   BAH2 = ( BAH >= 1 ) && ( BAH <= 3 ),
   BAH1 = ( BAH >= 4 ) && ( BAH <= 5 );
  
if( BAH2 )
{
   TakeProfit2 = 500;
}
   
if( BAH1 )
{
   TakeProfit2 = 1000;
}
   
Print( "take profit", TakeProfit2 );

And here is a another way of achieving the same result:

srand( GetTickCount() );

int
   BAH         = rand() % 5 + 1,
   TakeProfit2 = 0;

switch( BAH )
{
   case 1:
   case 2:
   case 3:
      TakeProfit2 = 500;
      break;

   case 4:
   case 5:
      TakeProfit2 = 1000;
      break;
}
   
Print( "take profit", TakeProfit2 );
nicholi shen
1895
nicholi shen  

In higher level languages this would be a simple task of assigning your values to a list and then passing the list to a random choice function. In MQL we can do the same thing, but it requires us to be slightly more verbose. 

void OnStart()
{
   srand(_RandomSeed);
   int numbers[] = {500, 500, 500, 1000, 1000};
   printf("%d is the random choice.", random_choice(numbers));
}

template <typename T>
T random_choice(T &array[])
{
   return array[rand() % ArraySize(array)];
}
prweza
426
prweza  
Fernando Carreiro:

Maybe I am misunderstanding your new question, but did I not just reply to that?

Maybe I should show the complete code for easier understanding:

And here is a another way of achieving the same result:

Thaks Fernando this this worked well, what is Tickcount function for?

Fernando Carreiro
4419
Fernando Carreiro  
prweza: Thaks Fernando this this worked well, what is Tickcount function for?

To help seed the randomization so that its sequence will be different every time you run the code. Without seeding it will always be the same sequence.

You can also use _RandomSeed, as used by @nicholi shen in his post #5. Please folow the link and read the documentation for it.

Also, please remember to read the documentation for which @Anthony Garot gave you the link in his post #2:

MathSrand (or srand)


Sets the starting point for generating a series of pseudorandom integers.

void  MathSrand(
   int  seed      // initializing number
   );

Parameters

seed

[in]  Starting number for the sequence of random numbers.

Return Value

No return value.

Note

The MathRand() function is used for generating a sequence of pseudorandom numbers. Call of MathSrand() with a certain initializing number allows to always produces the same sequence of pseudorandom numbers.

To ensure receipt of non-recurring sequence, use the call of MathSrand(GetTickCount()), since the value of GetTickCount() increases from the moment of the start of the operating system and is not repeated within 49 days, until the built-in counter of milliseconds overflows. Use of MathSrand(TimeCurrent()) is not suitable, because the TimeCurrent() function returns the time of the last tick, which can be unchanged for a long time, for example at the weekend.

Initialization of the random number generator using MathSrand() for indicators and Expert Advisors is better performed in the OnInit() handler; it saves you from the following multiple restarts of the generator in OnTick() and OnCalculate().

Instead of the MathSrand() function you can use the srand() function.

Documentation on MQL5: Predefined Variables / _RandomSeed
Documentation on MQL5: Predefined Variables / _RandomSeed
  • www.mql5.com
Predefined Variables / _RandomSeed - Reference on algorithmic/automated trading language for MetaTrader 5
To add comments, please log in or register