In this tutorial I have explain about how random number and LCM method. Here I have also provided a code to generate random number using LCM & built-in function in C++ with line by line explanation. To understand the code we must know about random number, pseudo random number and LCM method first.

## What is Random Number?

Random number is a number that is picked from a range randomly. That means to pick a random number you cannot follow any specific instruction. For example, picking up a lottery.

In a lottery draw only one person can be the first prize winner. So, who can be that? You cannot support someone just because he is your close friend. You should consider everyone is equal. So, you have to choice a winner in such way that every person have a possibility to win the prize. So, you mix up all tickets and pick one ticket unwillingly and tell that is the winner.

If you tell that one of the persons who have ticket with ticket number divisible by 5 is winner then that is not a random choice. A random number is probable equally everywhere.

## What is Pseudo Random Number?

If a random number or random sequence is statistically random but is derived from a known starting point then the random number or random sequence is called Pseudo Random Number. As the sequence is starting from a known number so the sequence will repeat after some of the instances. A pseudo random series may have all numbers in the range or not. It depends on the starting value, increment, multiplier etc. Pseudo-random numbers provide random values as necessity for processes that require randomness, such as creating test signals or for synchronizing sending and receiving devices in a spread spectrum transmission.

## Why is PRN Called Pseudo?

Pseudo Random Numbers are called pseudo because it is not actually a random sequence. It has a known starting number. It progress with two known value called increment and multiplier. For those reasons the series can repeat over and over. As the algorithm can repeat the sequence and the numbers are not entirely random so it is called pseudo random number.

## Formula for Pseudo Random Number Generator

There are several formula for pseudo random number generator. One of the most common method is Linear Congruential Method. Linear Congruential Method or LCM generates pseudo random number using a linear equation.

## Linear Congruential Method

A series of pseudo random number always start with a pre-defined value called Seed or Start value. There are three other parameters. If,

Seed Value = X_{0}

Multiplier = a

Increment = c

Modulus = m

then,

X_{i+1} = (a.X_{i} + c) % m; where i=0,1,2,3,4,………….

Each time the previous random value is multiplied with the multiplier and summed up with the increment then divided by the modulus to find the reminder as necessary random number.

## Limitations of Linear Congruential Method

- The sequence may be too short for some seed values. In this case, the seed value is called weak.
- For a larger there may have some nonuniform distribution.
- Values in the sequence may be correlated.
- If the multiplier is equal to the modulus all the numbers will be same in the series.
- If multiplier is 1 then this is a pure linear series (i.e. 3, 6, 9,…).

## C++ Code for Random Number Generator

```
#include<bits/stdc++.h>
using namespace std;
int main(){
cout<<"10 Random Numbers between 0-9."<<endl;
cout<<"Using Function:"<<endl;
for(int i=0; i<10; i++){
cout<<"X["<<i+1<<"]: "<<rand()%10<<endl;
}
cout<<"Using Linear Congruential Method:"<<endl;
int a = 7, c = 4, X[100], m=10;
X[0]=3;
for(int i=0; i<10; i++){
cout<<"X["<<i+1<<"]: "<<(a*X[i]+c)%m<<endl;
}
return 0;
}
```

## Explanation of Code

```
#include<bits/stdc++.h>
```

This line includes the standard c++ header file to get access required function.

```
using namespace std;
```

This line declares that the program is going to use the standard namespace.

```
int main(){
......
return 0;
}
```

The main function of the program. All of the instances will run inside this function.

```
cout<<"10 Random Numbers between 0-9."<<endl;
```

This line says to the user that it is going to print “10 Random Numbers between 0-9.” using built-in function LCM.

```
cout<<"Using Function:"<<endl;
for(int i=0; i<10; i++){
cout<<"X["<<i+1<<"]: "<<rand()%10<<endl;
}
```

This fragment prints 10 random number using c++ built-in function.

Here, we have used a for loop to iterate the process. The loop starts with i=0, increment i by 1 and end as soon as i become 10.

The loop prints the random number generated in each step. Here %10 (modulus) is used to wrap the number between 0 to 9.

endl is used to create a new line.

```
cout<<"Using Linear Congruential Method:"<<endl;
int a = 7, c = 4, X[100], m=10;
X[0]=3;
for(int i=0; i<10; i++){
cout<<"X["<<i+1<<"]: "<<(a*X[i]+c)%m<<endl;
}
```

This fragment prints 10 random number using linear congruential method.

Here we have assigned some variable required in LCM method.

Seed X_{0} = 3;

Multiplier a = 7;

Increment c = 4;

Modulus m = 10;

Here, we have used a for loop to iterate the process. The loop starts with i=0, increment i by 1 and end as soon as i become 10. As like in the function.

The loop prints the random number generated in each step with the formula described above.

Hope you liked this tutorial. So, please have a comment for some inspiration.