# Function part 4 (Random functions)

Estimated reading: 4 minutes 108 Views

Random functions in C++ are used to generate random numbers. There are two built-in random functions in C++: `rand()` and `srand()`.

The `rand()` function generates a random integer between 0 and RAND_MAX, where RAND_MAX is a macro that is defined in the `cstdlib` header file. The `srand()` function is used to seed the random number generator. If you don’t call `srand()`, the random number generator will be seeded with a default value.

Example 1:

```#include <iostream>
#include <cstdlib>

using namespace std;
int main()
{

cout << rand() << endl;

return 0;
}
```

This code prints a random integer to the console.

Example 2:

```#include <iostream>
#include <cstdlib>

using namespace std;
int main()
{
for (size_t i = 0; i < 10; i++)
{
cout << rand() << endl;
}

return 0;
}```

This code is a for loop that prints 10 random integers to the console.

Example 3:

```#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
for (size_t i = 0; i < 10; i++)
{
cout << rand()%10 << endl;
}

return 0;
}
```

This code generates a random number between 0 and 9 and prints it to the console. The modulus operator (%) returns the remainder of a division operation. So, the expression `rand()%10` will return the remainder of the operation `rand() / 10`. This will give us a number between 0 and 9.

Example 4:

```#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
for (size_t i = 1; i <= 10; i++)
{
cout << rand()%(30 - 20 + 1) + 20 << endl;
}

return 0;
}```

This code generates a random number between 20 and 30 and prints it to the console.

Example 5:

```#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
srand(time(0));
for (size_t i = 1; i <= 10; i++)
{
cout << rand()%(30 - 20 + 1) + 20 << endl;
}

return 0;
}```

The `srand(time(0));` function seeds the random number generator with the current time. This ensures that the random numbers are different each time the program is run.

The `srand()` function takes an integer seed as its argument and initializes the random number generator with that seed. The `time(0)` function returns the current time as a time_t value, which is an integer.

By seeding the random number generator with the current time, we are using a value that is constantly changing, which ensures that the random numbers are different each time the program is run.

It is important to seed the random number generator before using the `rand()` function. If you do not seed the random number generator, it will be seeded with a default value, which will result in the same sequence of random numbers being generated each time the program is run.

Example 6:

```#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
int x, y;
x = time(0);
for (size_t i = 0; i < 10000; i++)
{
cout << time(NULL) << endl;
y = time (0);
}
cout << "The token time:" << y - x << endl;
return 0;
}```

This code prints the current time 10000 times to the console and then prints the total time it took to do so.

The program works as follows:

1. The `int x, y;` declaration creates two integer variables, `x` and `y`.
2. The `x = time(0);` statement assigns the current time to the variable `x`.
3. The `for (size_t i = 0; i < 10000; i++)` loop iterates 10000 times.
4. On each iteration of the for loop, the following code is executed:
• The current time is printed to the console using the `cout << time(NULL) << endl;` statement.
5. The `y = time(0);` statement assigns the current time to the variable `y`.
6. The `cout << "The token time:" << y - x << endl;` statement prints the total time it took to print the current time 10000 times to the console.

The `time(NULL)` function is a synonym for the `time(0)` function.

You can use this code to measure the performance of your code or to calculate the elapsed time between two events.