How to generate a random number in a given range in C.
Input : Lower = 50, Upper = 100, Count of random Number = 5 Output : 91 34 21 88 29 Explanation: lower is the lower limit of the range and upper is the upper limit of the range. Output contains 5 random numbers in given range.
As C does not have an inbuilt function for generating a number in the range, but it does have rand function which generate a random number from 0 to RAND_MAX. With the help of rand () a number in range can be generated as num = (rand() % (upper – lower + 1)) + lower
Note : Output generated on each run may be different because number is generated randomly.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Generating Test Cases (generate() and generate_n() in C++)
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Select a random number from stream, with O(1) space
- How to check whether a number is in the range[low, high] using one comparison ?
- C Program for Number of elements with odd factors in given range
- How to print range of basic data types without any library function and constant in C?
- Using range in switch case in C/C++
- What happen when we exceed valid range of built-in data types in C++?
- C/C++ Program to find Prime Numbers between given range
- C/C++ Program to Find the Number Occurring Odd Number of Times
- Program for Sum of the digits of a given number
- What is the best way in C to convert a number to a string?
- How to count set bits in a floating point number in C?
- Convert a floating point number to string in C
- Converting string to number and vice-versa in C++
- Sorting 2D Vector in C++ | Set 3 (By number of columns)
- C Program to count number of lines in a file
- Maximum number of threads that can be created within a process in C
- How to bind to port number less than 1024 with non root access?
- Explicitly assigning port number to client in Socket
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.