Home » C Programs » C Program to Generate Random Numbers

C Program to Generate Random Numbers

In this program we can c program to generate random numbers. It will print the random numbers in the output. By using this program it will create a One time password in the ATM. 

Header File in C

The header for the C programming language’s General Purpose Standard Library, stdlib.h, declares a variety of utility functions for type conversions, memory allocation, process control, and other similar tasks. The header also defines a number of data types and macros.

Prototypes for standard input/output functions such as scanf/printf can be found in stdio. h. Without this file, it will be impossible to read keyboard input or write output to the screen. The name std(Standard)-i(input)-o indicates that it is a header file or library for Standard Input and Output (output). 

Rand Function in C Program

Rand returns normalised values (between 0 and 1) drawn from a uniform distribution by default. To change the distribution’s range to a new range, (a, b), multiply each value by the new range’s width, (b – a), and then shift each value by a.

For Loop Definition in C

A “For” Loop is used to iterate over a specific block of code a predetermined number of times. A for loop is a type of repetition control structure that allows us to write a loop that is executed a set number of times. The loop allows us to perform a n number of steps in a single line. 

C Program to Generate Random Numbers

//c program to generate random numbers
#include <stdio.h>
#include <stdlib.h>
int main() 
{
int a, n;
printf("Print the number1 to 100\n");
for (a = 1; a <= 10; a++) 
{
n = rand() % 100 + 1;
printf("%d\n", n);
}
return 0;
}

Output 

Print the number[1,100]
84
87
78
16
94
36
87
93
50
22