C Program to Generate Random Numbers in a Given Range Using rand() Function on Command Line

Here, You will know about “C Program to Generate Random Numbers in a Given Range Using rand() Function on Command Line“. Below, We will discuss how to do the task using this tutorial? Let’s move.

C Program to Generate Random Numbers in a Given Range Using rand() Function on Command Line

C Program to Generate Random Numbers in a Given Range Using rand() Function on Command Line

Program Code

// C program for generating a
// random number in a given range.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Generates and prints 'count' random
// numbers in range [lower, upper].
void printRandoms(int lower, int upper,
							int count)
{
	int i;
	for (i = 0; i < count; i++) {
		int num = (rand() %
		(upper - lower + 1)) + lower;
		printf("%d ", num);
	}
}

// Driver code
int main()
{
	int lower = 5, upper = 7, count = 1;

	// Use current time as
	// seed for random generator
	srand(time(0));

	printRandoms(lower, upper, count);

	return 0;
}

Final Words

We hope this article helps you to learn “C Program to Generate Random Numbers in a Given Range Using rand() Function on Command Line”. If you have any doubts about this topic then please let us know via the comment section. We will help you. Share this article with other C/C++ developers.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment