# C Program to Generate Random Numbers2 min read

In this C program section, we will learn how to generate random numbers in C programming. Let us start with random numbers.

## What is Randon Number?

We can define Random as something that happens without any conscious decision. Similarly, a random number is a number that is chosen unpredictably or randomly from the set of numbers.

During programming or creating a project, you may come through scenarios to generate numbers such as in dice games, lottery systems, etc. Although, there are in-built functions in C which makes it easy.

• `rand()`
• `srand()`

## `rand()` function

`rand()` in c programming is a library function that generates random numbers within a range [0, RAND_MAX]. To use `rand()`, we need to provide a header file `<stdlib.h>`. The return type of `rand()` is a random integer type.

#### Program: Generate the random numbers using the rand() function in C.

Output:

`Random number:: 1804289383 Random number:: 846930886 Random number:: 1681692777 `

#### Program: Generate 4 random numbers using the rand() function in C.

Output 1:

`4 Random Numbers are: 1804289383 846930886 1681692777 1714636915 1957747793`

Output 2:

`4 Random Numbers are: 1804289383 846930886 1681692777 1714636915 1957747793`

No matter how many times you execute this program, it will return the same sequence of random numbers on every execution of the programming.

## `srand()` function

`srand()` is a library function that sets the starting value for producing a series of pseudo-random integers. A `srand()` function cannot be used without using a rand() function. The `srand()` function is used to set the seed of the rand function to a different starting point.

It is used in the following manner in the programmer:

`int srand(unsigned int seed) `

Seed is an integer value for the new sequence in pseudo-random numbers.

#### Program: Generate the random numbers using the srand() function in C.

Output:

`Enter how many numbers you want: 5 5 Random Numbers are: 55 70 60 17 50`

Again after second execution:

`Enter how many numbers you want: 5 5 Random Numbers are: 36 70 91 3 2`

Now you can see that the program returns series of different random number after every execution.

### Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

### C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

### C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

### C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

### C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

### Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …