Generating random number in a range in C
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.
Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.