×

Difference between rand() and srand() function in C

What is the rand()?

The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a game in C using some random number from 1 to 9.

In this case, we can use the rand() function to create a game. In the case of programs, when the rand() function generates without calling the srand() function, the program can produce the same value when it is running. rand() function generates by some algorithm.

This algorithm gives a series of numbers unrelated to each other when the rand() and function are calls.

Example- When we generate the random function between 1 to 9, then we write the function like the below instruction:

Num = rand() % 9 + 1

Syntax:  

int rand(Void);

Parameters – The rand() function does not use any parameters.

Return value - The rand() function returns the pseudo number in RAND_MAX. The range of the return value is between 0 to RAND_MAX.

If we generate 10 random numbers by the C with the help of the rand() function in a loop, then whenever we compile and run the program, the output sequence is always the same. The program is created where the same sequence is in the random numbers.

Program –

#include <stdio.h> 
#include <conio.h> 
int main (void) 
{ 
Printf (“generated random numbers are: “);
for (int i = 0; i < 3; i++) 
printf (“%d”, rand()); 
return 0; } 

Output 

When the first time we generate the code, the output will be –

generated random numbers are: 123 567 456

When the second time we generate the code, the output will be –

generated random numbers are: 123 567 456

When the nth time we generate the code, the output will be –

generated random numbers are: 123 567 456

This is the program using the rand() function. The above program is generated the same sequence of a random number in the output whenever the program run. So the output is the same in every program.

What is srand()?

Strand() is another important function in C. srand() is the in-built function in C. srand() defined by the <stdlib.h> header file in the C program. The argument is passed like a seed.

In the program, if srand() is not called, then rand() seed set. If srand() = srand(1) then the program start. The seed value is used to srand() to generate the different pseudo functions. Any value of the seed is set the develop another starting point.

Syntax – int srand ( unsigned seed );

Parameters - The srand() function use parameter seed. The seed parameter uses to generate the pseudo-random integers.

Return value – The srand() function returns the pseudo-generated random number. This function should be seeded or reseeded. When anyone want to generate a new series of pseudo numbers.

Program –

#include <stdio.h> 
#include <stdlib.h> #include <time.h> 
int main (void)
 { 
 Srand (time (0)); 
Printf (“generated random numbers are: “); 
for (int i = 0; i < 3; i++) 
printf (“%d”, srand()); 
return 0; } 

Output – When the first time we generate the code, the output will be –               

generated random numbers are: 123 567 456

When the second time we generate the code, the output will be –

generated random numbers are: 127 547 436

When the nth time we generate the code, the output will be –

generated random numbers are: 523 467 156

This is the program using srand() function. The above program generates a different sequence of a random number in the output every time program runs.

So the main difference between the rand() and srand() function using C is the rand() function generates the same output every time of program execution; the srand() function does not generate the same result every time of program execution. The syntax, parameter all the values are different in the rand and srand.


Related Topics

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Pyramid Pattern in C

Pyramid Pattern in C A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected...

5 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

2 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.