Maximum value for srand ((unsigned) time (NULL));

2

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++;
       }
}
    
asked by anonymous 09.09.2016 / 01:47

2 answers

5

You can get the largest possible random implemented where you are using RAND_MAX . Only deployments and many old or bad (Windows) platforms are so low.

If you need numbers above the generated you have to create formulas to reach this level, just as it is done to obtain numbers within a certain limit.

See a example in ideone (2,147,483,647).

An example of a formula (not to say that it is the best):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LIMITE 1000

int myrand() {
    int r = 0;
    for (int i = 0; i < 2; i++) {
        r = (r << 15) | (rand() & 0x7FFF);
    }
    return r & 0xFFFFFFFF;
}

int main() {
    int vet[LIMITE];
    srand((unsigned)time(NULL));
    for (int x = 0 ; x < LIMITE; x++) {
          vet[x] = myrand();
    }
    for (int x = 0; x < LIMITE; x++) {
        for (int y = x + 1; y < LIMITE; y++) {
            if (vet[x] > vet[y]) {
                int aux = vet[x];
                vet[x] = vet[y];
                vet[y] = aux;
            }
        }
    }
    printf("\nVetor ordenado (ou nao): \n\n");
    for (int x = 0; x < LIMITE; x++) {
        printf("Numero %d: %d\n", x + 1, vet[x]);
    }
}

See working on ideone (it does not help much, in Windows you can see better).

    
09.09.2016 / 02:04
0

The numbers generated by rand() are pseudo - random, not random (after all, they are generated by an algorithm, do not have to be random).

Now that number 32767 is suspect, because it is the largest positive integer represented in 16 bits (in notation complement of 2).

Apparently the pseudorandom number generation algorithm works only with 16 bits in the environment you used for testing.

    
09.09.2016 / 02:01