MathRandom(0,10)

 

Hello. 

I've developed a tendency of requiring random numbers within  a specified range.

Has anyone ever come accross, or developed such a library? I could use some help, rather than creating one from scratch. 

Sample functionalities:

RandInt(1,8)

RandReal(0.01,10)

 
Nelson Wanyama:

Hello. 

I've developed a tendency of requiring random numbers within  a specified range.

Has anyone ever come accross, or developed such a library? I could use some help, rather than creating one from scratch. 

Sample functionalities:

RandInt(1,8)

RandReal(0.01,10)

What kind of required tendency of random numbers do you need??

Explain your case...

 
Nelson Wanyama:

Hello. 

I've developed a tendency of requiring random numbers within  a specified range.

Has anyone ever come accross, or developed such a library? I could use some help, rather than creating one from scratch. 

Sample functionalities:

RandInt(1,8)

RandReal(0.01,10)

What about (not tested!):

uint limLo = 100, limHi = 356, range = limHi - limLo;
uint rand = limLo + (GetTickCount() % range);   // or GetMicrosecondCount()

Check whether the limits are part of the range or not!

 
Carl Schreiber:

What about (not tested!):

Check whether the limits are part of the range or not!

This is a good implementational idea. I'd have to complicate things to prevent consecutive repetion for smaller ranges. Thanks!

 
Flavio Jarabeck:

What kind of required tendency of random numbers do you need??

Explain your case...

Being the innovative person that I am, say I have an enum enumerated from 0 to 24, which represent possible actions for my program to take. During training, I'd like to select random actions and make the program 'think' about the consequences, without actually taking the action. Having consecutive similar random values is inefficient, since a thinking about a spoon twice doesn't make it a plate. 

This is a sample case I encountered a day ago. Random number generators are available in python. I thought someone converted one of them, or any similar one, to MQL. I noticed a couple in the standard library, but they don't feature this option.

 
Flavio Jarabeck:

What kind of required tendency of random numbers do you need??

Explain your case...

Task:

Choose a random action from an enum of actions, enumerated from 0 to 54.

 

Hello

try using code below :

void OnInit() 
  { 
//--- Initialize the generator of random numbers 
   MathSrand(GetTickCount()); 
}

int OnCalculate(const int rates_total, 
                const int prev_calculated, 
                const datetime &time[], 
                const double &open[], 
                const double &high[], 
                const double &low[], 
                const double &close[], 
                const long &tick_volume[], 
                const long &volume[], 
                const int &spread[]) 
  { 
        int rand=0;
 
        //--- A remainder in the division by 55 will return a value from 0 to 54
        rand=MathRand()%55; 
         
      return(rates_total); 
  } 
 

The choices could be weighted based on previous outcomes though . Im guessing you are taking care of evaluation later and its more of a "dont stop evolving" issue .

So what Seyed Soroush Bagher Nezhad did 

 

Have luck with my CRandom class.

This class is a wrapper class around the high-quality 32-bits PCG generator. This RNG has an output range of 2^32 which means it can generate 4,294,967,296 possible values.

It achieves excellent statistical performance with small and fast code, and small state size.

https://www.mql5.com/en/code/25843


If you would like to go with the standard MathRand()

//+------------------------------------------------------------------+
//| Random integer in the range [min, max)                           |
//| Generates random numbers with a uniform distribution (bias-free).|
//| http://www.pcg-random.org/posts/bounded-rands.html               |
//+------------------------------------------------------------------+
int MathRandInt(const int min, const int max)
  {
   int range = max - min;
   int limit = (32768 - range) % range;
   int r;  do{ r = MathRand(); }while (r < limit);
   return r % range + min;
  }
Class CRandom
Class CRandom
  • www.mql5.com
The standard random number generator of mql has a fairly limited number of possible values from 0 to 32767 (15-bits usable, 2^15 = 32,768 values).  32-bits PCG Generator This class is a wrapper class around the high-quality 32-bits PCG generator. This RNG has an output range of 2^32 which means it can generate 4,294,967,296 possible values. A...