Advertisement

info on rand() and other simple things

Started by August 21, 2001 02:45 PM
4 comments, last by Sage13 23 years, 6 months ago
Can some one please explain to me what this Fn. does and give me a very small example of how I can use it. rand() Also, what''s going on here. I know this is a fn. passing variables into it, but the last part I don''t understand. What is the " :p " ? Draw_String(player_x,0,":p"); I would also like to know somehting about these header files: #include #include And i think this fn. Has somehting to do with the time.h thing srand((unsigned)time(NULL)); I''m getting this code from a windows game programming for dummies book by the way. Thanx
rand() returns a pseudo random number. srand() just seeds the random number generater so that it doesn't return the same set of values each time the app is run.

Example (console app):
    int main(void){   int random;   srand(time(0)); //seed random number generator with                   //the current time   random = rand() % 100;   printf("A random number between 0 and 100 is %d", random);   getchar();   return 0;}    


For your other question, I can't tell. Next time you post, don't use < or > in your post, as they will be treated as HTML. If you want them to show up you must use &lt; and &gt;.

For Draw_String... how am I supposed to know what an application-defined function parameter means? Based on the name though, I would guess it is the actual text string to be drawn.

Edited by - Midnight Coder on August 21, 2001 4:03:09 PM
Advertisement
The function rand returns a random number based on the "random seed". The random seed is set (only once) by calling srand with an ever changing value (otherwise you get the same numbers every time). What''s an easy way to get an ever changing number? Give it the time. The likelyhood that you''ll have the exact same time happen twice is pretty slim () so you have a low probability of having predictable numbers.

Draw_String isn''t a standard function, so I couldn''t tell you what it''s doing. I''ve never read the book, and don''t own it, but it should explain it somewhere in there.

The header ctype.h is for converting types to others, and identifying what type of thing something is. Some functions defined in ctype.h are:

isalnum: Tests if a character is alphanumeric
isalpha: Tests if a character is a letter
isdigit: Tests if a characer is a number
islower: Tests if the character is lowercase
tolower: Converts the character to lowercase
toupper: Converts the character to uppercase
And so on.

The header time.h is mainly very simple time functions.

[Resist Windows XP''s Invasive Production Activation Technology!]

.-Well...

.-rand(), this function returns a pseudo random number(int?). Have a look at the help for exact info.

.-DrawString(player_x,0,""); . Without reading its implementation, I bet it Draws a string(many characters,phrase), at the position(player_x,0).

.-"" . HAHA, turn your head 90º. is a picture with two little eyes, a mouth and a tongue out of it!. "" around it makes it a string type, const char*.


.-srand(). It sets the "seed" for the rand() function,rand returns the same value when the same seed is set. A way to make it random is to set a seed that can´t be forseen.Setting the current tick is random enough...

Hope to help!

What the hells!
What the hells!
THANX!

So with time.h, I can like...what. make a dot scroll every few seconds or something?

-Sage13
Yes, time.h does have some functions you could use to time the scrolling of a dot on the screen so it happens every so many milliseconds.

This topic is closed to new replies.

Advertisement