+ 2

how to generate random number in c or cpp with range?

15th Nov 2016, 4:41 PM
Rana Ahmad
Rana Ahmad - avatar
7 Answers
+ 6
As far as I know, we don't have any method to generate random numbers within range. Workaround for this, rand() % range; This will generate the random numbers within specified range. Hope it will be useful for you
15th Nov 2016, 4:46 PM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
+ 4
Is this a correct way #include <iostream> using namespace std; int main() { //random number min + rand() % (max - min) return 0; }
15th Nov 2016, 5:01 PM
Umar Farouq (Humarh_dharnarh)
Umar Farouq (Humarh_dharnarh) - avatar
+ 4
One last thing for you all; don't forget to: #include <time.h> and srand(time(0))
16th Nov 2016, 3:11 AM
Keto Z
Keto Z - avatar
+ 3
actually, it is not that use min + rand() % (max - min)
15th Nov 2016, 4:56 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 2
thank u so much
15th Nov 2016, 4:46 PM
Rana Ahmad
Rana Ahmad - avatar
+ 2
Yes, I assumed that min is 0. If it's not 0 then this is the proper solution.
15th Nov 2016, 5:05 PM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
+ 1
how can generate random coordinates???
19th Nov 2016, 6:03 AM
sabahat
sabahat - avatar