EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Random Number Generator in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Random Number Generator in C

Random Number Generator in C

Introduction to Random Number Generator in C

In order to generate the expected output, the program must need the proper input. Usually, the inputs are provided by the user but sometimes the program has to pick the input by itself. For instance, to get the current timestamp the application uses an inbuilt function to fetch it from the system. In the same way, sometimes we need to have application generate any random number which could be processed further to get the supposed output. Though it looks random to the user the programming language offers us the mechanism to define the range of the random number. In this article, we will see the program implementation of random numbers generation using the C programming language. We will be focusing on the inbuilt function that is provided by C in order to generate a random number.

What is Random Number Generator Functions in C?

There are several approaches to generate the random number using any of the programming language. One can define the function of their own way to estimate or generate the random number while there are inbuilt functions in any of the programming language that generates the random number. In the C programming language, we have a function called rand, which helps in generating the random number. This function comes predefined in C and can be implemented in the program using stdlib.h header file. The developer needs to mention the stdlib.h header file in the beginning of the program in order to leverage the rand function. Everytime this function is called, it generates a totally random number. Based on the requirement one can generate the number belongs to integer, float or double data type. It can be simply used in the program using rand() function.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Though the rand function is supposed to generate the random value, it stuck to generate the same value every time the program is executed and it may happen due to the constant seed value. If the requirement is to have the new random number generated every time the program executes than we have to make sure that the seed should change whenever the program runs. Time is something that keeps on changing and can also be considered as something that can help in getting a random seed value every time and to use time in the program we have to use time.h header file.

Generation Integers

The rand() function is used to generate a random number. Every time it is called, it gives a random number. If the developers add some logic with it, they can generate the random number within a defined range and if the range is not defined explicitly, it will return a totally random integer value. The rand() function in C could be used in order to generate the random number and the generated number is totally deleting seed. A seed is a value that is used by rand function to generate the random value. If the seed value is kept on changing, the number generated will new every time the program is compiled else it will return the same value every time that was generated when the program was executed first. In order to generate the Below is the program to generate the integer random number.

Program

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int rand_num;
srand(time(0));
printf("The randomly generated number is”);
rand_num = rand();
printf("%d\n", rand_num);
}
}

Output:

The randomly generated number is 1804289383.

In this program, we have used time.h header file which is used to leverage the system time in generating the random number. As the time changes every time, the value of seed will change every time the program will be executed, helping us to generate a random number every time the program is executed. Rand_num is the variable that is used to store a randomly generated number. The function rand() generates a random number which is assigned to the variable rand_num. As we didn’t define the return value of the random number explicitly, it will give us an integer number.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Generating Float Point Numbers

The approach to generate the random float value is similar to the approach for generating the integer number. The only difference is, we will need to explicitly define that the value we are expecting from the rand function should be a float. The float value usually consumes more space in storage as compared to the short int. The program that we have written in the above for random integer value generation will be the same as we are going to write here. The only difference will be an explicit data type definition. Similar to the last program, we have used time.h header file here as well to let it contribute in random float number generation. If this header file is not included in the program, it will give the same value every time the program. Is executed. Below is the program for random float value generation.

Program

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
float rand_num;
srand(time(0));
printf("The randomly generated float number is ”);
rand_num = (float) rand();
printf("%f", rand_num);
}
}

Output:

Random Number Generator in C Output

In this program, we have used (float) which is used to explicitly define that the value returned from the rand function should be float in nature. As the ran_num variable is also defined with a float data type, it will be able to handle the float number which usually has six digits after the decimal point when generated in the C programming language. While printing the float value, we have used %f as it is something that has to be taken care of while printing the float value.

Conclusion

To enhance the randomness of the number, one can leverage mathematical expressions. Also, using logic one can also define the range of numbers under which they want the number to be generated randomly. The feature to generate random rubber is provided by all of the programming languages and used in the actual application based on the requirement. In order to ensure the strong randomness of the number, we have to make sure that the seed that rand function is used to generate the random value should be very random and new every time the program runs.

Recommended Articles

This is a guide to Random Number Generator in C. Here we discuss the function, generation integers and generating float point numbers in C. You can also go through our other suggested articles to learn more –

  1. Random Number Generator in Python
  2. Random Number Generator in C++
  3. Random Number Generator in C#
  4. Random Number Generator in JavaScript

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More