How to make an random int greater than a number and less than another number? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to make an random int greater than a number and less than another number?

I want to make an random number to be in an area of int. How can I do this with a mathematical solve?

29th Sep 2018, 3:36 PM
ld programs
2 Answers
+ 3
In C++: rand() % (max - min + 1) + min; where max is the upper limit and min the lower limit. Note that +1 is there to include max into the range of numbers you can get. This works only for positive integers though. Another approach would be to use objects from the random library (distributions), because here you can specify the range when constructing the object: http://www.cplusplus.com/reference/random/
29th Sep 2018, 4:09 PM
Shadow
Shadow - avatar
0
thx
1st Oct 2018, 11:52 AM
ld programs