By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

code

P: 7
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;
}
Mar 15 '07 #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
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.
Mar 15 '07 #2

P: 4
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.

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
Mar 15 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.