### HOW TO CREATE TEXT GAMES USING C! : PART 1 (Using the rand() function)

- The most important characteristic of any game or match(both digital and physical) is its
**randomness**in which actions or moves take place. That is what makes a game interesting - Consider the classic rock paper scissors game! Here the fun lies in randomness of selection of options by both the players even though the number of choices is limited
- So if we could somehow get this characteristic into our program... Then we are very well on our way to making exciting games!

**So how do we bring randomness into our program?**

**Well, for this very purpose, we have a predefined function under the 'stdlib.h' header file and the function name is rand()**

**:**

__functionality of rand()__The rand() function generates a random pseudo-number between 0 and 32767

So how can we use this feature of rand() for creating a game? Below is a simple example to explain it all!

**:**

__Example__- Let us say we have a dice.
- The outcomes of a dice are only 6, and they are 1,2,3,4,5, and 6.
- So let us write a program to generate a number randomly between 1 to 6!
- And yes, we use the rand() function!
- But the problem is... as stated earlier, rand() function generates a number between 0 to 32767... but we need output only between 1 and 6!
- So how do cut down the results of rand() down to 1 to 6?
- The modulus operator comes to our rescue!!
- BUT HOW? See the code below, and I'm sure you'd understand if you haven't figured out how by now

```
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a;
printf("rolling the dice....");
a = (rand()%6)+1;
printf("you got %d",a);
return 0;
}
```

- rand()%6 would always produce a number between 0 and 5 and adding 1 to it would produce the necessary outcome that is inclusively between 1 and 6! So, (rand()%6)+1 is ideal for our purpose.
- But there's a problem with the above code, and that is .... it would only produce the same result every time. Try running the code in your compiler 4-5 times, and every time you'd end up with the same output. Try using a loop, the same set of outputs is repeated every time!
- The reason behind this is that the numbers are generated by a mathematical algorithm which when given a starting number (called the '
**seed**'), always generates the same sequence of numbers. - If you are interested, know more about it here
- to keep things simple, just add this line of code at the beginning of your code

```
srand ( time(NULL) );
```

- Just remember to add the above line.
**This makes use of the computer's internal clock to control the choice of the seed. Since time is continually changing, the seed is forever changing. Remember, if the seed number remains the same, the sequence of numbers will be repeated for each run of the program.**

- so finally we have the code:

```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int a;
srand(time(NULL));
a = (rand()%6)+1;
printf("%d",a);
return 0;
}
```

- So the above code generates a random number in desired range every time we execute the program!
- Now you have the necessary element to create a simple text game.... try using this knowledge to create one! If you still can't figure out, jump to the part 2 of this series of post by clicking the link below...In part 2, a simple text game is developed with the help of information of this post

So the above program is an electrical dice! try using it to play a game of monopoly... just kidding :)

## Comments

## Post a Comment