×

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

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

Decimal to Binary in C

What is a decimal number? A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits...

3 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Addition of Matrix in C

The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article,...

3 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.