code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • molatelo
    New Member
    • Mar 2007
    • 7

    code

    wat does this code do?
    #include <stdlib.h>
    #include <stdio.h>

    int main( void )
    {
    int j, s[100];

    int srand(unsigned int s);

    j = 1 + (rand() % 100);
    printf( " %d\n", j );
    return 0;
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    It declares the function srand (which is unnecessary because it is declared in stdlib.h) then it calls rand without having seeded the random number generator and performs some minor maths on the result finally printing the result which will be the same every time because the random number generator was not seeded.

    Comment

    • Sandro Bosio
      New Member
      • Mar 2007
      • 4

      #3
      Originally posted by Banfa
      It declares the function srand (which is unnecessary because it is declared in stdlib.h)
      Indeed the declaration of srand in stdlib.h is void srand(unsigned) , and not int srand(unsigned) , so this code does not compile to me.

      Originally posted by Banfa
      then it [...] performs some minor maths on the result
      which is obviously to obtain a random number between 1 and 100, which as noticed will not be so random after all.

      Sandro

      Comment

      Working...