PDA

Click to See Complete Forum and Search --> : I'm new to this language, please bear with me


Sep 30th, 2000, 02:38 PM
Hi, I have just started to teach myself C++ programming for DOS (I use the DJGPP compiler). I need to find a way to generate random numbers (its for a number-guessing game that I have set myself a task to create!). I need the numbers to be within a certain range for example 0-10000.

I cannot seem to make any of the srand() or rand() commands work.

Which headers do i need to #include ?

Greatful for any enlightenment :)

It's a great language to learn isn't it, very satisfying when it works (not often for me).

HarryW
Sep 30th, 2000, 04:10 PM
Try stdlib.h, I think that's where the rand() function is defined.

HarryW
Sep 30th, 2000, 04:13 PM
Oh yeah, and to get one in a range, use this:

randomNumber = rand % (max-min) + min

where max and min define the range of values you want.

parksie
Sep 30th, 2000, 05:10 PM
srand() seeds the random number. Basically, it allows the random numbers to be more "random". If you seed it with something like the current time, you're much more likely to get different numbers.
Then call rand() as normal.

Oct 1st, 2000, 03:59 AM
thanks guys ill give it a try. :)