-
How do I...?
OK,
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!!!
-
Hiya!
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);
srand();
something to that effect. i hope this points you in the right direction.
cj
-
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)