# Generates a random number.

Your conversation partner speaks foreign language? Automatic translation will help you!
40
2011.09.10 22:20

I'm trying to create a simple function that generates a random number using seconds.

I'm having trouble getting the value of seconds.The what is wrong?

this is the function.

int NewNumberRand(void)

{

MqlDateTime timetoRand;
int timeSeconds;
int result=0;

timeSeconds = 2*timetoRand.sec;

Print("2*timetoRand.sec ", timeSeconds);
Print("timetoRand.sec ", timetoRand.sec);

if(timeSeconds>0 || timeSeconds <= 10)
{
result = 1;
}

if(timeSeconds>10 || timeSeconds <= 20)
{
result = 2;
}

if(timeSeconds>20 || timeSeconds <= 30)
{
result = 3;
}

if(timeSeconds>30 || timeSeconds <= 40)
{
result = 4;
}

if(timeSeconds>40 || timeSeconds <= 50)
{
result = 5;
}

if(timeSeconds>50 || timeSeconds <= 60)
{
result = 6;
}

if(timeSeconds>60 || timeSeconds <= 70)
{
result = 7;
}

if(timeSeconds>70 || timeSeconds <= 80)
{
result = 8;
}

if(timeSeconds>80 || timeSeconds <= 90)
{
result = 9;
}

if(timeSeconds>90 || timeSeconds <= 100)
{
result = 10;
}

if(timeSeconds>100 || timeSeconds <= 110)
{
result = 11;
}

if(timeSeconds>110 || timeSeconds <= 120)
{
result = 12;
}
Print("result  ", result);
return (result);
}
Documentation on MQL5: Standard Constants, Enumerations and Structures / Data Structures / Structure of the Date Type
• www.mql5.com
Standard Constants, Enumerations and Structures / Data Structures / Structure of the Date Type - Documentation on MQL5
490
2011.09.11 07:51
FForex:

I'm trying to create a simple function that generates a random number using seconds.

I'm having trouble getting the value of seconds.The what is wrong?

this is the function.

int NewNumberRand(void)

{

MqlDateTime timetoRand;
int timeSeconds;
int result=0;
...
return (result);
}

How do you fill the timetoRand variable?

Anyway why don't you use MathRand() function?

40
2011.09.12 07:00
fireflies:

How do you fill the timetoRand variable?

Anyway why don't you use MathRand() function?

Because I want the function to return 1.2,..., 12.

I solved this way.

I split the value of (max)rand in 12 equal intervals then 12 generating rand is in one of these intervals the function will return the corresponding value.

int NewNumberRand(void)
{
int randNumber = rand();

int result=0;

//Print("randNumber   ", randNumber);
//Print("rand() ", rand());

if(randNumber>0 && randNumber <= 2730)
{
result = 1;
}

if(randNumber>2730 && randNumber <= 5460)
{
result = 2;
}

if(randNumber>5460 && randNumber <= 8190)
{
result = 3;
}

if(randNumber>8190 && randNumber <= 10920)
{
result = 4;
}

if(randNumber>10920 && randNumber <= 13650)
{
result = 5;
}

if(randNumber>13650 && randNumber <= 16380)
{
result = 6;
}

if(randNumber>16380 && randNumber <= 19110)
{
result = 7;
}

if(randNumber>19110 && randNumber<= 21840)
{
result = 8;
}

if(randNumber>21840 && randNumber <= 24570)
{
result = 9;
}

if(randNumber>24570 && randNumber <= 27300)
{
result = 10;
}

if(randNumber>27300 && randNumber<= 30030)
{
result = 11;
}

if(randNumber>30030 && randNumber<= 32767)
{
result = 12;
}
//Print("result  ", result);
return (result);
}

490
2011.09.12 07:28
FForex:

Because I want the function to return 1.2,..., 12.

I solved this way.

I split the value of (max)rand in 12 equal intervals then 12 generating rand is in one of these intervals the function will return the corresponding value.

Wouldn't it be simpler to just use

MathRand()%12 +1

40
2011.09.12 19:35
fireflies:

Wouldn't it be simpler to just use

MathRand()%12 +1

Your idea is actually simpler and more efficient than mine.
Another idea I'm having problems and you may have the solution is as follows.
resceber my role should the pair (EURUSD), the lot, and the percentage desired.

And Derevo return the value in EURUSD (money). See the prototyping code.

double MoneyPercentLot(const string msymbol, double lot, double percent)

{

double result=0

???......

return(result);

}

490
2011.09.14 08:16
FForex:
Your idea is actually simpler and more efficient than mine.
Another idea I'm having problems and you may have the solution is as follows.
resceber my role should the pair (EURUSD), the lot, and the percentage desired.

And Derevo return the value in EURUSD (money). See the prototyping code.

double MoneyPercentLot(const string msymbol, double lot, double percent)

{

double result=0

???......

return(result);

}

Could you explain a bit more? Percentage of what and what return value are you looking for?