You can't disagree with the fact that you have played Ludo at least once in your lifetime. If you have played it on your phone or any electronic device, you must have noticed that a random number from one to six appears every time someone rolls the dice.

The random function is a way of generating a random number every time in C. In this article you will learn about the random function in C programming language.

**What is Random Function in C?**

The random function generates random numbers within specified ranges. In C, it's facilitated by two built-in functions: rand() and srand().

As these functions are implemented inside the stdlib library in C, in order to use these functions, we need to include the â€˜stdlibâ€™ header file as follows:

`#include<stdlib.h> `

Letâ€™s now see rand() and srand() functions for using random functions in more detail.

**rand() function**

This function basically generates random numbers every time it is called inside a program. In general, the range of the generated number is 0 to RAND_MAX. Here RAND_MAX is any numeric value greater than or equal to 2^{31}-1 in most of the modern compilers. However, this may vary based on the compiler. But, the generated numbers will always be positive. It is apparent that the return type of this function is int.

### Syntax of random() function in C

`int rand(void)`

### Generate the 3 random numbers using the rand() function

Letâ€™s try to print 3 random numbers

**Output:**

```
The 1 random number is: 1804289383
The 2 random number is: 846930886
The 3 random number is: 1681692777
```

Letâ€™s try running this same program 2 more times and look at the output:

1st time:

```
The 1 random number is: 1804289383
The 2 random number is: 846930886
The 3 random number is: 1681692777
```

2nd time:

```
The 1 random number is: 1804289383
The 2 random number is: 846930886
The 3 random number is: 1681692777
```

Well, this is not something that we expected. Every time executed, it gives the same random numbers. So, the reason why this happened is that the compiler is not actually generating a random number every time, instead rand() function is deterministic. Deterministic means that it has specific rules by which it produces numbers. It pretends to be generating random numbers. It already has a sequence of numbers that it periodically displays repeatedly. Still, we never know what may happen as the length of that sequence is very large, so there never seems to be any repetition of numbers.

In reality, what it is doing is a sort of transformation.

Transformation can be like this:

Let a number be x. Here, â€˜xâ€™ is the seed. You will learn about seed later in this article.

Step 1: Multiply â€˜xâ€™ with 7

Step 2: Add 59 to the product

Suppose the number is 74

After step 1: 74*7=518

After step 2: 518+59=577

To generate the next number

After step 1: 577*7=4039

After step 2: 4039+59=4098

Of course, the way it has been actually implemented is not exactly like this, but I hope you got an idea.

Now, suppose you don't want the same sequence every time. You want it to be completely random for that we have another function srand().

### Generate 10 random numbers from 1 to 100 using rand() function

Lets try to generate 10 random numbers between 1 and 100.

**Output:**

```
10 Random Numbers between 1 and 100:
84
87
78
16
94
36
87
93
50
22
```

**Explanation**

In this program, srand(time(NULL)); seeds the random number generator using the current time so that each time you run the program, you get different random numbers. rand() % 100 + 1; generates a random number between 0 and 99, then adds 1 to shift the range to 1-100.