1
#include<stdio.h>
#include <time.h>
#include <stdlib.h>
int aleatorio(){
srand(time(NULL));
return rand() % 10 + 1;
}
main(){
int numero1 = aleatorio();
int numero2 = aleatorio();
printf("%d %d",numero1, numero2);
}
I have a project to do and I will use a function that generates random numbers, so I decided to go doing tests on it and the function always returns the same number, even if I have put the seed inside Rand(). Even if the seed has been placed within a function, the generated number should not be different already as each time I call it a new seed is placed. I’d like you to help me.
basically by the fact that you do srand twice with the same value. Do srand only once, otherwise you reset Seed to each function call (ie two calls on the same time() will always result the same value.
– Bacco
Retire
srand(time(NULL));
of its function. It must be executed only once at the beginning of the program and not every call of the functionaleatorio
.– anonimo