Q:

C program to generate random numbers within a range

0

C program to generate random numbers within a range

Random numbers just numbers that lie within a range and any of the numbers can occur.

In programming, we come through a lot of scenarios where we need to generate random numbers. Like for dice games, lottery system, etc. In the c programming language, there is an inbuilt method to take care of this.

srand() and rand() functions are used to generate random numbers. Now, let's dig deep into these methods and understand their working.

rand() function

The rand() function in the C programming language is used to generate a random number. The return type is of rand() function is an integer.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C code to generate a random number

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    printf("Random number is:  %d ", rand());
    return 0;
}

Output

Random number is:  1804289383

srand() function

The srand() function is used to set the starting value for the series of random integers. You can use the srand() to set the seed of the rand function to a different starting point.

The parameters that are passed into the srand() function are the starting values for the rand method.

C code to generate random numbers using srand()

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    srand(time(0));
    printf("%d ", rand());
    return 0;
}

Output

1370632410

Generating random numbers within a range

The rand() function generates random numbers that can be any integer value. But, to generate random numbers within a specific range, we have a formula that returns a random number between given ranges.

Formula:

    number = (rand() % (upper - lower + 1)) + lower

Program to generate random numbers from 1 to 6

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int lower = 1, upper = 6, count = 10;

    srand(time(0));

    printf("The random numbers are: ");
    for (int i = 0; i < count; i++) {
        int num = (rand() % (upper - lower + 1)) + lower;
        printf("%d ", num);
    }

    return 0;
}

Output

The random numbers are: 1 5 4 4 4 2 3 2 4 3

Explanation:

This program declares a variable num that generates a random number between 1 to 6. for doing this we will initialize the lower limit of the rand to 1 and The upper limit of the rand function to 6. this will generate a random number between 1 to 6 using the formula.

Program to generate random number between 1 to 100

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int lower = 1, upper = 100, count = 10;

    srand(time(0));

    printf("The random numbers are: ");
    for (int i = 0; i < count; i++) {
        int num = (rand() % (upper - lower + 1)) + lower;
        printf("%d ", num);
    }

    return 0;
}

Output

The random numbers are: 50 22 52 69 24 98 39 31 77 97

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C programming Basic Input, Output, if else, Ternary Operator based Programs

Similar questions


need a help?


find thousands of online teachers now
C Example to subtract two integers without using M... >>
<< C program to find area and perimeter of the rectan...