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.


MORE

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More