I was reading about random numbers not being so random and I saw one way out was to feed a seed with srand((unsigned)time(NULL) );
To test I generated a vector of 100,000 positions and ordered it, but it only has the number 32767
random, that is, some numbers are repeated several times.
Did I do something wrong or the maximum value for srand
is that same?
Follow the code in C
:
#include
#include
#include
int vet[100000];
int main()
{
int x, y, j, aux;
srand((unsigned)time(NULL) );
for(x=1 ; x <= 100000; x++){
vet[x]= rand();
// printf("Número %d: %d\n",x, vet[x]); //Tirei essa linha só pra não poluir a tela com os gerados aleatoriamente e depois com os ordenados
}
for(x=0; x<=100000; x++ ){
for( y=x+1; y<=100000; y++ ){
if( vet[x] > vet[y] ){
aux = vet[x];
vet[x] = vet[y];
vet[y] = aux;
}
}
}
printf("\nVetor ordenado (ou nao): \n\n");
x=1;
for(j=1;j<=100000;j++)
{
printf("Numero %d: %d\n",j, vet[x]);
x++;
}
}