C/C++: srand() and rand() to get random numbers

We can simply design a 6/49 lottery selector by randomly get numbers from 1 to 49. We will use srand() and rand().

srand() initializes random generator by argument seed. Normally, we use time() as seed.

srand( (unsigned int) time( (time_t *) NULL) );

rand() is function call to generate numbers which initialized some distinctive value using srand(). We can get number from 1 to 49 by mod 49 and plus 1.

(rand() % 49) + 1;

Discussion Area - Leave a Comment




Spam Protection by WP-SpamFree Plugin