Im just learning about random numbers, but my program generates the same numbers each time i run it. I read i need to use this function, but im not sure how, could someone give me an example syntax in which it's used? Thanks
Printable View
Im just learning about random numbers, but my program generates the same numbers each time i run it. I read i need to use this function, but im not sure how, could someone give me an example syntax in which it's used? Thanks
Code:srand((unsigned)time(NULL));
srand() seeds the random number generator. The number you pass to it decides which sequence of numbers you'll get (send the same number twice and you'll get the same sequence again). Most people get this number from time(NULL) which will return the nr of seconds since 00:00 hours, Jan 1, 1970 UTC (will practically guarantee a new number every time).
so, once when the program starts:
and then to get a random number:Code:srand(time(NULL));
Code:int n = rand();
Thanks guys, i love this forum, lol