views
Random Function in C - javatpoint
In this topic, we will learn about the random function and how we can generate the random number in the C programming language. As we know, the random function is used to find the random number between any two defined numbers. In the C programming language, the random function has two inbuilt functions: rand() and srand() function. Let's understand these functions in the C language.
In the C programming language, the rand() function is a library function that generates the random number in the range [0, RAND_MAX]. When we use the rand() function in a program, we need to implement the stdlib.h header file because rand() function is defined in the stdlib header file. It does not contain any seed number. Therefore, when we execute the same program again and again, it returns the same values.
The rand() function returns the random integers whose range from 0 to RAND_MAX. The RAND_MAX is a symbolic constant that defines in stdlib.h header file, whose value is greater but less than 32767 depending on the C libraries.
Let's write a program to get the random number using rand() function.
num.c
Output
Let's consider a program to generate 5 random number using rand() function in C programming language.
random.c
Output
2nd execution of the program:
3rd execution of the program
As we can see in the above output, it returns the same sequence of random numbers on every execution of the programming code.
Let's consider a program to find the random number in C using rand() function.
rand_num.c
Output
The srand() function is a C library function that determines the initial point to generate different series of pseudo-random numbers. A srand() function cannot be used without using a rand() function. The srand() function is required to set the value of the seed only once in a program to generate the different results of random integers before calling the rand() function.
seed: It is an integer value that contains a seed for a new sequence of pseudo-random numbers.
Let's write a program to get the random numbers using srand() function in C.
srandNum.c
Output
2nd execution of the program:
As we can see in the above Output, it returns different sequences of random numbers on every execution of the programming code.
Let's write a program to get the random numbers using srand() with time() function.
srand_time.c
Output
Let's write a program to get the seed value and random numbers using srand() function.
srand_time.c
Output
2nd execution of the program:
3rd execution of the program:
As we can see in the above Output, when we executed the same program again and again with different seeds values, it displays the different sequences of a random number from 1 to 6.
Let's create a program to use stadlib header file to get the random number using random function in C.
func.c
Output
Let's consider a program to print the float random numbers in C.
random1.c
Output
SoapUI
RPA
Manual T.
Cucumber
Appium
PostgreSQL
Solr
MongoDB
Gimp
Verilog
Teradata
PhoneGap
Aptitude
Reasoning
Verbal A.
Interview
Company
AI
AWS
Selenium
Cloud
Hadoop
ReactJS
D. Science
Angular 7
Blockchain
Git
ML
DevOps
DBMS
DS
DAA
OS
C. Network
Compiler D.
COA
D. Math.
E. Hacking
C. Graphics
Software E.
Web Tech.
Cyber Sec.
Automata
C
C++
Java
.Net
Python
Programs
Control S.
Data Mining
Hindi100
Lyricsia
Website Development
Android Development
Website Designing
Digital Marketing
Summer Training
Industrial Training
College Campus Training
Address: G-13, 2nd Floor, Sec-3
Noida, UP, 201301, India
Contact No: 0120-4256464, 9990449935
© Copyright 2011-2018 www.javatpoint.com. All rights reserved. Developed by JavaTpoint.