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
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));
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel
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();
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Thanks guys, i love this forum, lol