Generating two random numbers

how do i generate two random numbers: for example num1 and num2
but I want num1 to always be greater than num2?

srand((unsigned)time(0));
num1 = rand() % 40 + 1;
num2 = rand() % 30 + 1;


.....
Thanks!
Well, many different ways. If num 1 is a random number between 1 and 40, you can make num2 a random number between 41 and some other high number.

Or if they're both something like a random number between 1-100. You can just make an if statement checking if num2 is higher, if it's not you can generate another random value until it is higher than num1.
Can you give an example in how to do the second suggestion.
because I have tried a few times but can figure it out.
Thanks!
1
2
3
4
5
6
7
8
srand((unsigned)time(0));
num1 = rand() % 100 + 1; 

do
{
    num2 = rand() % 100 + 1;

}while(num2 <= num1); // exits when num2 is bigger than num1 


I should think the easiest way would be to just swap the two numbers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <random>

int random(int min, int max) {
	static std::random_device device{};
	static std::default_random_engine engine{ device() };
	std::uniform_int_distribution<int> distribution{ min, max };
	return distribution(engine);
}

int main() {

	int number_one = random(0, 3);
	int number_two = number_one + random(1, 2);

	return 0;
}
Topic archived. No new replies allowed.