Random Number generator in C

 In this article, we will create a random number generator using C which will be able to generate -

  • Random numbers
  • Random numbers in a given range
  • Random numbers, which are multiples of a given number


Random Number generator
Random number generator


The following header files are used -

  • stdio.h - For standard input or output stream.
  • stdlib.h - For standard library functions.
  • time.h - to get date and time info.


The following functions are used - 

  • rand() -  to generate pseudo-random integers in the range 0 to 32767.
  • srand() -  the rand() function will generate the same random integer every time we run the program. Hence to generate different random numbers, we use srand() function by passing time as the seed.


The following user-defined functions are created -

  • rnos - This function generates multiple random numbers. It takes no parameters as input and doesn't return any value. It just displays the generated random numbers. 
  • rnosmul - This function generates random nos. which are multiples of the user's input number. This function takes in an integer value (user's input no.) as input and returns no value. It just displays the generated random numbers. 
  • rnosrang - This function generates random nos. in a given range. It takes starting range and ending range as input and displays the generated random numbers within the range.It doesn't return any value.

Code - 

//Random number generator by Vishruth Codes
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//Function to generate multiple random numbers
void rnos()
{
    int qty;
    printf("\nEnter the count: ");
    scanf("%d",&qty);
    srand(time(0));
    printf("The random nos. are: ");
    for(int i=0;i<qty;i++)
    {
        printf("%d, ",rand());
    }
}

//Function to generate multiple random numbers which are multiples of a given number
void rnosmul(int n)
{
    int qty;
    printf("\nEnter the count: ");
    scanf("%d",&qty);
    srand(time(0));
    printf("The random nos. are: ");
    for(int i=0;i<qty;i++)
    {
        printf("%d, ",(rand()*n));
    }
}

//Function to generate multiple random numbers of a given range
void rnosrang(int s,int e)
{
    int qty, rem;
    printf("Enter the count: ");
    scanf("%d",&qty);
    rem=e-s;
    printf("\nThe random nos. are: ");
    //Checking if the given count exceeds the count of possible nos. that can be generated within the given range
    if(rem<qty)
    {
        printf("\nINVALID Count!");
        exit(0);
    }
    else
    {
    srand(time(0));
    for(int i=0;i<qty;i++)
    {
        //Formula for generating random nos. whthin a give range = lower_limit + (rand()%(upper_limit - lower_limit))
        printf("%d, ",(s+(rand()%(e-s))));
    }
    }
}

int main()
{
    int n, mult, srang, erang;
    printf("\nSelect - \n1. for generating random numbers\n2. for generating random nos. which are multiples of a number.\n3. for generating random numbers belonging to a particular range.\n: - ");
    scanf("%d",&n);
    if(n==1)
        rnos();
    else if(n==2)
    {
        printf("\nEnter the number: ");
        scanf("%d",&mult);
        rnosmul(mult);
    }
    else if(n==3)
    {
        printf("\nEnter the starting & ending range: ");
        //Input lower limit and upper linit
        scanf("%d",&srang);
        scanf("%d",&erang);
        rnosrang(srang,erang);
    }
    else{
        printf("\nInvalid Input");
        exit(1);
    }
    return 0;
}


Subject - 

In the above example, I have implemented a C code for generating random numbers.


Basic Features - 

  • This program can generate random numbers, random numbers in a given range, and random numbers which are multiples of a user input number.
  • The count of random numbers generated is set by the user.

Working - This Code is self-explanatory. 


Output - 

Random number generator in c++
Output



Thanks for reading. If you found any errors or have suggestions to improve the article, please mention them in the comments.

Post a Comment

0 Comments