Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1260

 
Vitaly Muzichenko:

Can you tell me what to write to get random 6-digit numbers?

void OnStart()
{
   srand(GetTickCount());
   for(int i = 0; i < 10; i++)
   {
      uint random = rand_32() % 1000000;  // %1000000 --> ограничим 6-ю знаками 
      printf("random = %u", random);
   }
}
//+------------------------------------------------------------------+
uint rand_32()
{
   return(((uint)rand()) << 16 | rand());
}
//+------------------------------------------------------------------+
 
Alexey Viktorov:

And what was the result? I'm not claiming infallibility...))

As a result I had to leave it as there were less than 6 characters

if(v>=min && v<=max)

Igor Makanu:

I'll try it tonight.

 
Igor Makanu:

The 5-digit ones slip through


Anyway, problem solved, thank you all!

 
Vitaly Muzichenko:

5-digit numbers are slipping through

Yeah, they will.

thought it was only a 6-digit challenge.

as an option to generate the first digit with rand()%10

 
Igor Makanu:

Yes they will.

thought that the task is only no more than 6 digits

as an option to generate the first character with rand()%10

Put it back in the code.

if(v>=min && v<=max)

that's enough, the function is called a couple times a day anyway

 
Vitaly Muzichenko:

Put it back in the code.

that's enough, the function is called a couple of times a day anyway.

no

simpler way

write it that way

uint random = (rand() % 9 + 1) * 1000000 +   // первый знак
                    rand_32() % 100000;      // ограничим 5-ю знаками
 
Igor Makanu:

no

It's simpler than that.

so write it down.

Well, you could randomly dial each sign (six pieces) and collect the number)

 
Valeriy Yastremskiy:

Well, you could randomise each character (six pieces) and collect the number)

Yes, but it would probably be a bad rand - you need to test

I think, it is easier to shift 16-bit rand() and add more rand() to low bits = get 32-bit - my example rand_32()

 
Igor Makanu:

Yes, but it would probably be a bad rand - I need to test

I think it's easier to shift a 16-bit rand() and add another rand() to the low bits = get a 32-bit rand_32() - my example

I agree. Either test or tweak the generator logic, which is usually more expensive than the task.

 
Valeriy Yastremskiy:

I agree. It's either testing or tinkering with the generator logic, which is usually more expensive than the task at hand.

)