Question about the rand() function

Hello.

I want to make a program that, given the minimum value and the maximum value, generates a random number within that range.

I made that with this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <conio.h>
#include <ctime>

using namespace std;

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

	int min = 0, max = 2;

	int val = ((max + 1) - min) + min;

	int num = rand() % val;

	cout << "Random number: " << num << endl;
	cout << "Value: " << val << endl;


	_getch();
	return 0;
}


If you see, the min is 0 and the max is 2.
The second COUT (that prints the final value) prints 3

Now, if I change the range from 0-2 to 1-2, the second cout still prints 3

But... if the value is always 3 that means that this code

rand() % val

is equivalent to

rand() % 3

But the point here is that, despite the value is the same, the program has 2 different behaviors

In the first "0-2" case, it prints these values {0,1,2}
In the "1-2" case, it prints these ones {1,2}

How can the program work in 2 different ways with the same value (3)???
val = max + 1 - min + min

You subtract min and then you add min so you end up with the same value as you had before.

val = max + 1
Last edited on
If the max is 7 how do I determine the min now?

val = max + 1

val = 7 + 1;

val = 8;

How do I say the range has to be 0 -> 7, and how do I say 1 -> 7, or whatever other minimum value, min -> 7
The number of possible values is max - min + 1, so use that to generate a random number in the range 0 to val-1. Then add min.

example:
min = 5
max = 7
possible values are 5, 6 and 7, that is 3 possibilities.
generate a random number in the range 0, 1, 2 and then add min.

Thanks, but look at this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// THIS CODE WORKS
int main()
{
	srand(time(NULL));

	int min =1, max = 2;

	int val = (max - min + 1);

	int num = rand() % val+min;

	cout << "Random number: " << num << endl;

	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// THIS ONE DOESN'T
int main()
{
	srand(time(NULL));

	int min =1, max = 2;

	int val = (max - min + 1) + min;

	int num = rand() % val;

	cout << "Random number: " << num << endl;

	return 0;
}


The first code also prints 0, which is NOT in range.

Why? The only difference between the two is that in the first code I add min after rand() %. In the second code i add min in the int var assignment.

Why one works and one doesn't? They aren't the same?
No, they are not the same. The compiler interprets rand() % val+min as (rand() % val) + min.
Last edited on
The first code also prints 0, which is NOT in range.

do you mean the second code?

Second code, line 8
int val = (max - min + 1) + min;
equivalent to
int val = (max + 1);

This line will generate a number in the range 0 to max.
int num = rand() % val;

They aren't the same?
No. The second code does not make use of min at all.
Last edited on
The compiler interprets rand() % val+min as (rand() % val) + min
I thought the + operator had greater precedence than the % operator


So in short we can say that

int NumberInRange = rand() % max + min;

right?
Last edited on
So in short we can say that

int NumberInRange = rand() % max + min;


Did you test it?
Yes and it works, except when the minimum is 0 and the maximum is 1 though <.<
Last edited on
How 'bout when the minimum is 95 and the maximum is 100?
Actually you screwed that over.

Oh my gosh guys, what's the right algorithm for this?

I'm confused
Do you understand how the % operator work?
It returns the rest of the division btween rand() and what comes after.
Topic archived. No new replies allowed.