When I studied pseudocode, I learned that when you call a function and create a variable, it only "exists" when I call that function, for example.
funcao teste():
x = 10
retorna x
In case, when I called my test function, it would create the variable x
, and after it returned it would erase it.
I am studying C ++ and tried to do this function to generate random numbers.
int gerar_numeros() {
srand(time(NULL));
int x = rand() % 100;
return x;
}
and to assign in the vector this excerpt in the main()
function.
for (int c = 0; c <= 10; c++)
{
vetor[c] = gerar_numeros();
}
But the problem is that it only changed the numbers when I ran the script again, eg the first time all the vector values was 5, the second time 10 and so on , then I changed the logic to that one and it worked out.
void gerar_numeros(int vetor[]) {
srand(time(NULL));
//int x = 1 + rand() % 100;
for (int c = 0; c <= 10; c++)
{
vetor[c] = rand() % 100;
}
In the above logic it is as if the variable x
was stored in memory, so it always returns the same number.