How do I generate a random number between 3-5?

I'm trying to generate a random number between 3-5 but I keep getting values like 7 and 6 when I run it. Why is that? I'm using the correct headers and the srand((0)) so that shouldn't be a concern. When I did this with min as 0 and max as 2 it worked fine, never left the range. So why does this give me a number outside my range sometimes when the parameters for min is 3 and max is 5? Trying to generate a random num between 3-5. Thanks! :)
1
2
3
4
5
int randNum(int min, int max) {

	return rand()% max % min;

}
First off you need to declare:
 
srand(time(NULL));

for it to be truly random.
try this:
1
2
3
4
5
6
7
int randNum(int min, int max)
//the parameters must be min = 3, max = 3;
{
int x = rand() % min +  max;
return x;
}


that will generate 3-5;
Last edited on
closed account (E0p9LyTq)
min = 3;
max = 3;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstdlib>
#include <ctime>

int randNum(int min, int max)
{
   return rand() % max + min;
}

int main()
{
   srand(time(0));

   for (int i = 0; i < 25; i++)
   {
      std::cout << randNum(3, 3) << "  ";
   }
}


5  4  3  3  5  4  4  5  3  4  3  4  3  5  3  3  4  3  3  5  3  3  4  4  5
Don't rely on the coincidence that there are three possible values and the minimum number is also three.

The range of possible values is (max - min + 1)
for example min = 3, max = 5
the three possible values are 3, 4, 5
(max - min + 1) = 5 - 3 + 1 = 3


hence use rand() % (max - min + 1)
Then add min.
vaultDweller, you shouldn't use rand() to generate random numbers, because it is an outdated function. See this article: http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/

Instead, use the Mersenne Twister. Here's an example of how to use it: http://www.cplusplus.com/reference/random/mersenne_twister_engine/operator%28%29/

Chervil has the correct formula for random number generation. In order to generate a random number between min and max inclusive, use

unsigned randomNumber = ( generator() % (max - min + 1) ) + min;

(If you named your std::mt19937 object "generator", that is.)

Here's why this formula is the way it is. If you just use generator(); on its own, without using the modulus operator, or subtracting anything, it will generate a random 32-bit integer (that is, there are 2^32 possible numbers it can generate). But you want random numbers in a certain range, so you use generator() % max;. This gives you random numbers in the range from 0 to max-1, since the modulus operator returns the remainder of the random number divided by max, in this example (if you don't know how modulus works, go learn how it works, then come back to this).

If you want max to be inclusive, then write generator() % (max + 1);. Now, this will generate random numbers in the range from 0 to max. But, now you want a minimum. It might seem simple enough to add min to the entire thing. Your code is now ( generator() % (max + 1) ) + min;. But, you aren't done! This code now generates random numbers in the range min to max + min. Instead of only shifting the minimum up by min, you've shifted the entire range up by min. We need to correct for this in the code, by doing this:

( generator() % (max - min + 1) ) + min;

Now, this is correct. Notice that the term max - min + 1 is the width of the range of numbers. For example, if max is 15, and min is 10, max - min + 1 is 6, the number of numbers inbetween 10 and 15, including 10 and 15. So, we are first restricting the range of random numbers to a certain length, and then shifting this whole range up by min, so that the lowest possible value is min, and the highest possible value is max. Try this out on a number line if you can't understand the explanation in text.

EDIT: I can't believe std::uniform_int_distribution slipped my mind. That is probably a lot easier to use than std::mt19937, for your purposes. Here's an example for how to use it: http://www.cplusplus.com/reference/random/uniform_int_distribution/operator%28%29/
Last edited on
Topic archived. No new replies allowed.