Thread: How do I...?

  1. #1
    Registered User Master_Rondal's Avatar
    Join Date
    Oct 2002

    How do I...?

    I am interested in game programming. But the only language i feel comfortable with is C...I have made a lengthy RPG using if-else statements and function calls...i was wondering how to use C's random function to make a random interger variable value between say 1 and 100 and have the user guess at it until they got it...and when they got it...the number would change...thus eliminating the "once i guess the number...the game is useless..."
    that i get from my old one...i need to know how to use the random function...HELP!!!

  2. #2
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002


    You wouldhave to use the function call rand() for randoming calling the same random number. srand() is calling random random numbers nut you will have to specify the integer towards the range of 0-100.

    ex. if (x>0 ||x<100);

    something to that effect. i hope this points you in the right direction.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    First you need to call srand(time(NULL)). This will seed the random generator with a number which decides which random numbers you get. You can seed it with a constant value also, but then you get the same random numbers all the time (it's not actually random numbers, they are calculated by a very unpredictable algorithm).

    When you want a random number, call rand(). This will return a number between 0 and RAND_MAX (32767 in my compiler). If you want a smaller number, use the modulus operator ( % ).

    If you want a number 0 - 9:
    (rand() % 10)

    If you want a number 0 - 100:
    (rand() % 101)

    If you want a number 50 - 100:
    ((rand() % 51) + 50)

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed