Results 1 to 5 of 5

Thread: I'm new to this language, please bear with me

  1. #1
    Guest

    Unhappy

    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).

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Try stdlib.h, I think that's where the rand() function is defined.
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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.
    Harry.

    "From one thing, know ten thousand things."

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Guest
    thanks guys ill give it a try.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width