Hi everyone
could anyone tell me how to generate a random integer number between 0 and 9.How to use s_rand() and rand() ?I think these functions are suitable but i dont know how to use them.Thanks for reading
Hi everyone
could anyone tell me how to generate a random integer number between 0 and 9.How to use s_rand() and rand() ?I think these functions are suitable but i dont know how to use them.Thanks for reading
This question is asked very often. Search the forums.
There's an FAQ on it: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
The short answer is: seed the random number with srand() (and probably time()); and get a random number with rand(), restricting its value to 10 with %.
BTW, only call srand() once.Code:#include <time.h> /* for time() */ #include <stdlib.h> /* for rand() and srand() */ int n; srand(time(NULL)); n = rand() % 10; /* generate a number from 0 - 9 */
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.