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

Asked

Viewed 1,192 times

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.

  • 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"

  • 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.

  • 1

    The seed does not seem to be related to the problem, but the compiler.

  • I am using GNU GCC Pilot.

  • The ideone too, but the problem is not only being GCC, there is a lot of variable there that interferes.

  • 1

    The fix on access to arrays was not to solve your problem with random numbers, it was a general guideline.

Show 2 more comments

2 answers

5


You can get as random as possible implemented where you are using with RAND_MAX. Only old or bad (Windows) implementations and platforms are so low.

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

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference. (2.147.483.647).

An example formula (doesn’t mean it’s 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]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference. (does not help much, in Windows can see better).

  • It replaces my vet[x]= Rand(); by vet[x]= RAND_MAX; and I did a test with 10 positions, but now all 10 positions generated 32767

  • Ahh, I ran your example of Ideone on my Codeblocks and the result was the same 32767

  • I didn’t say put the RAND_MAX, I said you can check as much as you can with it. Now we know you’re using an old/bad implementation. And that you will have to use formulas to achieve higher values than the RAND_MAX.

  • 1

    Apparently, on Windows (even 64 bits) RAND_MAX is even 32767... http://stackoverflow.com/questions/10074879/rand-4000000000ul-giving-only-small-values

  • Closed. I’m on W7

  • 5

    @Maximilianomeyer in this case you can call Rand more than once. When so, comment from the system on question tb. An example to generate larger numbers would be rand() * RAND_MAX + rand(), or rand() << 15 | rand() in the specific case of the 32767. Qq way, would have to check the RAND_MAX to decide the best strategy according to the environment.

  • 1

    @Bacco: the formula would be better rand() * (RAND_MAX + 1) + rand()

  • 1

    @pmg would indeed be correct, otherwise RAND_MAX would have 1/32767 more chance, and would lack a number on the list yet :). I was quick to exemplify, and did not take care of the details.

Show 3 more comments

0

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

Now, this number 32767 is suspicious, as it is the largest positive integer represented in 16 bits (in 2-bit complement notation).

Apparently the pseudo-random number generation algorithm works with only 16 bits, in the environment you used for testing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.