2
I was reading about the random numbers not being so random and I saw that one way out was to feed a seed with the srand((unsigned)time(NULL) );
To make tests I generated a vector of 100,000 positions and ordered it, but the same only has until the number 32767
random, ie some numbers are repeated several times.
I did something wrong or the maximum value for the srand
that’s the one?
Follows the code in C
:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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++;
}
}
Your code is wrong in accessing arrays. An array of 100000 elements can be accessed with indexes 0 to 99999.
– zentrunix
I made this change, but it didn’t help. I put it to start with 1 because I didn’t want it to appear "Number 0: value x"
– Evilmaax
Wow. I ran my Ideone algorithm and I ran it good. I’m on my college computer and the time is wrong here. I read that the seed is based on time, maybe that’s it. I’ll try my note at home.
– Evilmaax
The seed does not seem to be related to the problem, but the compiler.
– Bacco
I am using GNU GCC Pilot.
– Evilmaax
The ideone too, but the problem is not only being GCC, there is a lot of variable there that interferes.
– Maniero
The fix on access to arrays was not to solve your problem with random numbers, it was a general guideline.
– zentrunix