+ 1

Is there a function in c to get a random value between two values.

Is there a way to get a random value between two values in C. The random() function is only usable to get a random value without limits. I know that this can be done with random() function and conditional loops. I just need to know wether this can be done through a single function

4th Sep 2020, 10:27 AM
Mahela Dissanayake
Mahela Dissanayake - avatar
4 Answers
+ 2
What is c++ doing in tags then?
4th Sep 2020, 10:42 AM
Abhay
Abhay - avatar
+ 2
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ srand(time(0)); int i=0; while(i<10){ printf("%d\n",(rand()%5)+3); i++; } return 0; } Mod something returns a remainder less than that value so in this case you will get a random number 0 to 4 if you want to generate a number between some values just add that value to random number ,like adding 3 here generates a value starting at 3 till 7
4th Sep 2020, 11:17 AM
Abhay
Abhay - avatar