×

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.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 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.

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.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 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.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Floyd’s triangle in C

Floyd’s triangle in C Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming...

4 minutes read.