//+------------------------------------------------------------------+
//| RandomNumbers1.mq4 |
//| Copyright © 2008, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#define RAND_MAX 32767.0
//flags for the extended randstr function
#define RAND_STR_CAPITOL_LETTERS 0x00000001 //65 through 90
#define RAND_STR_LOWER_CASE_LETTERS 0x00000002 //97 through 122
#define RAND_STR_NUMBERS 0x00000004 //48 through 57
#define RAND_STR_SYMBOLS_1 0x00000008 //32 through 47
#define RAND_STR_SYMBOLS_2 0x00000010 //58 through 64
#define RAND_STR_SYMBOLS_3 0x00000020 //91 through 96
#define RAND_STR_SYMBOLS_4 0x00000040 //123 through 126
#define RAND_STR_NON_PRINTING 0x00000080 //1 through 31 and 127
#define RAND_STR_EXTENDED_ASCII 0x00000100 //128 through 255
//use this first function to seed the random number generator,
//call this before any of the other functions
void initrand()
{
//srand((unsigned)(time(0)));
MathSrand(TimeLocal());
}
//generates a psuedo-random integer between 0 and 32767
int randint0()
{
//return rand();
return(MathRand());
}
//generates a psuedo-random integer between 0 and max
int randint1(int max)
{
//return int(max*rand()/(RAND_MAX+1.0));
int r=max*MathRand()/(RAND_MAX+1.0);
return(r);
}
//generates a psuedo-random integer between min and max
int randint2(int min, int max)
{
if (min>max)
{
return (max+((min-max+1)*MathRand()/(RAND_MAX+1.0)));
}
else
{
return (min+((max-min+1)*MathRand()/(RAND_MAX+1.0)));
}
}
//generates a psuedo-random double between 0.0 and 0.999...
double randdouble0()
{
return (MathRand()/((RAND_MAX)+1));
}
//generates a psuedo-random double between 0.0 and max
double randdouble1(double max)
{
return (randdouble0()*max);
}
//generates a psuedo-random double between min and max
double randdouble2(double min, double max)
{
if (min>max)
{
return (randdouble0()*(min-max)+max);
}
else
{
return (randdouble0()*(max-min)+min);
}
}
string randstr0(string &str, int length)
{
//make we were passed a valid pointer
//if (str=="")
//{
// return (0);
//}
//make sure the string is supposed to contain something
if (!length)
{
return (0);
}
//put random characters into the string, give both
//upper and lower case numbers an equal chance at
//being used
for (int x=0;x<length-1;x++)
{
if (!randint1(1))
{
str=str+CharToStr(randint2(65,90));
}
else
{
str=str+CharToStr(randint2(97,122));
}
}
//null terminate the string
//str[x]=0;
return (str);
}
string randstr1(string str, int length, int flags)
{
//make we were passed a valid pointer
//if (!str)
//{
// return 0;
//}
//make sure the string is supposed to contain something
if (!length)
{
return (0);
}
//if none of the flags were used then we set flags to use just upper and lower case
if ((!flags&0x00000001)&&(!flags&0x00000002)&&(!flags&0x00000004)&&
(!flags&0x00000008)&&(!flags&0x00000010)&&(!flags&0x00000020)&&(!flags&0x00000040)
&&(!flags&0x00000080)&&(!flags&0x00000100))
{
flags=RAND_STR_LOWER_CASE_LETTERS|RAND_STR_CAPITOL_LETTERS;
}
int t[9]={0,0,0,0,0,0,0,0,0};
int i=0;
//each pass of the loop the flags are checked and for each flag that is used
//then a random character in the flags range is added to a temporary array of
//characters, after all the flags are checked a random character from the
//temporary array is chosen to be inserted into the string
for (int x=0;x<length-1;x++)
{
i=0;
if (flags&0x1 !=0)
{
t[i]=randint2(65,90);
i++;
}
if (flags&0x2 !=0)
{
t[i]=randint2(97,122);
i++;
}
if (flags&0x4 !=0)
{
t[i]=randint2(48,57);
i++;
}
if (flags&0x8 !=0)
{
t[i]=randint2(32,47);
i++;
}
if (flags&0x10 !=0)
{
t[i]=randint2(58,64);
i++;
}
if (flags&0x20 !=0)
{
t[i]=randint2(91,96);
i++;
}
if (flags&0x40 !=0)
{
t[i]=randint2(123,126);
i++;
}
if (flags&0x80 !=0)
{
if (!randint1(32))
{
t[i]=127;
i++;
}
else
{
t[i]=randint2(1,31);
i++;
}
}
if (flags&0x100 !=0)
{
t[i]=randint2(127,255);
i++;
}
str=str+CharToStr(t[randint1(i-1)]);
}
//null terminate the string
//str[x]=0;
return (str);
}
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
//----
initrand();
string str1;
string str2;
string str3;
string str4;
string str5;
str2=randstr0(str1, 12);
str3=randstr1(str3,12, RAND_STR_LOWER_CASE_LETTERS);
str4=randstr1(str4,12, RAND_STR_NUMBERS);
str5=randstr1(str5,12, RAND_STR_SYMBOLS_1 );
double r1=randdouble2(1.2,5.3);
Comment(str1+"\n"+str2+"\n"+str3+"\n"+str4+"\n"+str5+"\n"+r1);
//----
return(0);
}
//+------------------------------------------------------------------+
/*
//use this first function to seed the random number generator,
//call this before any of the other functions
void initrand()
{
srand((unsigned)(time(0)));
}
//generates a psuedo-random integer between 0 and 32767
int randint()
{
return rand();
}
//generates a psuedo-random integer between 0 and max
int randint(int max)
{
return int(max*rand()/(RAND_MAX+1.0));
}
//generates a psuedo-random integer between min and max
int randint(int min, int max)
{
if (min>max)
{
return max+int((min-max+1)*rand()/(RAND_MAX+1.0));
}
else
{
return min+int((max-min+1)*rand()/(RAND_MAX+1.0));
}
}
//generates a psuedo-random float between 0.0 and 0.999...
float randfloat()
{
return rand()/(float(RAND_MAX)+1);
}
//generates a psuedo-random float between 0.0 and max
float randfloat(float max)
{
return randfloat()*max;
}
//generates a psuedo-random float between min and max
float randfloat(float min, float max)
{
if (min>max)
{
return randfloat()*(min-max)+max;
}
else
{
return randfloat()*(max-min)+min;
}
}
//generates a psuedo-random double between 0.0 and 0.999...
double randdouble()
{
return rand()/(double(RAND_MAX)+1);
}
//generates a psuedo-random double between 0.0 and max
double randdouble(double max)
{
return randdouble()*max;
}
//generates a psuedo-random double between min and max
double randdouble(double min, double max)
{
if (min>max)
{
return randdouble()*(min-max)+max;
}
else
{
return randdouble()*(max-min)+min;
}
}
char* randstr(char* str, unsigned long length)
{
//make we were passed a valid pointer
if (!str)
{
return 0;
}
//make sure the string is supposed to contain something
if (!length)
{
return 0;
}
//put random characters into the string, give both
//upper and lower case numbers an equal chance at
//being used
for (unsigned long x=0;x<length-1;x++)
{
if (!randint(1))
{
str[x]=(char)randint(65,90);
}
else
{
str[x]=(char)randint(97,122);
}
}
//null terminate the string
str[x]=0;
return str;
}
//flags for the extended randstr function
#define RAND_STR_CAPITOL_LETTERS 0x00000001 //65 through 90
#define RAND_STR_LOWER_CASE_LETTERS 0x00000002 //97 through 122
#define RAND_STR_NUMBERS 0x00000004 //48 through 57
#define RAND_STR_SYMBOLS_1 0x00000008 //32 through 47
#define RAND_STR_SYMBOLS_2 0x00000010 //58 through 64
#define RAND_STR_SYMBOLS_3 0x00000020 //91 through 96
#define RAND_STR_SYMBOLS_4 0x00000040 //123 through 126
#define RAND_STR_NON_PRINTING 0x00000080 //1 through 31 and 127
#define RAND_STR_EXTENDED_ASCII 0x00000100 //128 through 255
char* randstr(char* str, unsigned long length, unsigned long flags)
{
//make we were passed a valid pointer
if (!str)
{
return 0;
}
//make sure the string is supposed to contain something
if (!length)
{
return 0;
}
//if none of the flags were used then we set flags to use just upper and lower case
if ((!flags&0x00000001)&&(!flags&0x00000002)&&(!flags&0x00000004)&&
(!flags&0x00000008)&&(!flags&0x00000010)&&(!flags&0x00000020)&&(!flags&0x00000040)
&&(!flags&0x00000080)&&(!flags&0x00000100))
{
flags=RAND_STR_LOWER_CASE_LETTERS|RAND_STR_CAPITOL_LETTERS;
}
char t[9]={0};
int i=0;
//each pass of the loop the flags are checked and for each flag that is used
//then a random character in the flags range is added to a temporary array of
//characters, after all the flags are checked a random character from the
//temporary array is chosen to be inserted into the string
for (unsigned long x=0;x<length-1;x++)
{
i=0;
if (flags&0x1)
{
t[i]=randint(65,90);
i++;
}
if (flags&0x2)
{
t[i]=randint(97,122);
i++;
}
if (flags&0x4)
{
t[i]=randint(48,57);
i++;
}
if (flags&0x8)
{
t[i]=randint(32,47);
i++;
}
if (flags&0x10)
{
t[i]=randint(58,64);
i++;
}
if (flags&0x20)
{
t[i]=randint(91,96);
i++;
}
if (flags&0x40)
{
t[i]=randint(123,126);
i++;
}
if (flags&0x80)
{
if (!randint(32))
{
t[i]=127;
i++;
}
else
{
t[i]=randint(1,31);
i++;
}
}
if (flags&0x100)
{
t[i]=randint(127,255);
i++;
}
str[x]=t[randint(i-1)];
}
//null terminate the string
str[x]=0;
return str;
}
*/
Comments