I have to generate a pair of random integers between 1-50. I then need to have my program generate five other numbers with the same range as the two random integers. I know how to generate the first pair but Iam having trouble creating the generation for the other 5. This is C++ by the way.
Asked
Active
Viewed 2,096 times
-3
-
Do you have code for the first part? where did you get stuck on the second part? Show us some code. – Caesar Nov 11 '13 at 19:27
-
1This previous thread [How does modulus and rand() work?](http://stackoverflow.com/questions/19553265/how-does-modulus-and-rand-work) covers both options for C++ and C++11. Make the code modular and once you generate the first range then use the same code to generate the next range. In C++11 it is much simpler. – Shafik Yaghmour Nov 11 '13 at 19:30
-
possible duplicate of [C++ random float number generation](http://stackoverflow.com/questions/686353/c-random-float-number-generation) – John Dibling Nov 11 '13 at 19:32
-
@Caesar #include
#include – user2980422 Nov 11 '13 at 19:34using namespace std; int main() { int num1 int num2 rand()%50=num1; rand()%50=num2; return 0; } sorry i dont know how to write organized code on here -
1@user2980422 if you are going to add code to your question, the comments is not the right place. Edit your question and add your code there. – Shafik Yaghmour Nov 11 '13 at 19:52
4 Answers
1
Assuming that I understand your problem, the following approach works for me.
Note that the 5 numbers will have the same absolute difference given by delta:
int delta = abs(pairOne.x - pairOne.y);
but you didn't mention whether or not x > y or y > x was a constraint (a simple modification can enable this). The code simply finds random numbers with the guaranteed difference (delta) as shown below.
#include <iostream>
#include <random>
struct RandPair {
int x;
int y;
};
int main()
{
// A Mersenne Twister pseudo-random generator:
typedef std::mt19937 CppRNG;
// Seed the generator:
uint32_t seed_val = 0;
CppRNG RandomGenerator;
RandomGenerator.seed(seed_val);
int min = 1;
int max = 50;
std::uniform_int_distribution<uint32_t> uniformMinMax(min, max);
RandPair pairOne;
RandPair otherPairs[5];
pairOne.x = uniformMinMax(RandomGenerator);
pairOne.y = uniformMinMax(RandomGenerator);
std::cout << pairOne.x << "; " << pairOne.y << std::endl;
int delta = abs(pairOne.x - pairOne.y);
int count = 0;
while (count < 5) {
int r1 = uniformMinMax(RandomGenerator);
int r2 = uniformMinMax(RandomGenerator);
if ( abs(r1 - r2) == delta && count < 5 ) {
otherPairs[count].x = r1;
otherPairs[count].y = r2;
count++;
}
}
for (int i = 0; i<5; i++) {
std::cout << "delta: " << delta << "; " << otherPairs[i].x << "; " << otherPairs[i].y << std::endl;
}
return 0;
}

Bruce Dean
- 2,798
- 2
- 18
- 30
-
-
@user2980422, Ok if I understand what you want, the above edit works, check it out. – Bruce Dean Nov 11 '13 at 20:14
-
http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful – Griwes Nov 11 '13 at 20:39
-
@Griwes, Oh yes, great point. See my edit above, thanks for pointing this out! – Bruce Dean Nov 11 '13 at 21:20
1
This solution requires #include <random>
and a C++11 capable compiler.
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> r1(1, 50);
int first = r1(mt), second = r1(mt);
if (first > second) std::swap(first, second);
std::uniform_int_distribution<int> r2(first, second);
for (int i = 0; i < 5; ++i) {
std::cout << r2(mt) << '\n';
}

A.B.
- 15,364
- 3
- 61
- 64
-1
int a, b, low, high;
a = rand()%50 + 1;
b = rand()%50 + 1;
low = min(a,b);
high = max(a,b);
int ar[5];
int range = high-low+1;
for(int i=0; i<5; ++i) {
ar[i] = rand()%(range) + low;
}

Shubham
- 935
- 1
- 7
- 25
-
http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful – Griwes Nov 11 '13 at 20:40
-1
Something like this should work:
void printRandomNumbers( int low, int high )
{
// Seed the random number generator.
srand( time(0) )
// Generate 2 random numbers between low and high (inlcusive).
int a = ( rand() % ( high-low ) ) + ( low + 1 );
int b = ( rand() % ( high-low ) ) + ( low + 1 );
// Find a new low and new high.
if ( a <= b )
{
low = a;
high = b;
} else {
high = a;
low = b;
}
printf("The new low is: %d\nThe new high is: %d\n", low, high);
// Print 5 numbers inside the new range.
for ( int i = 0; i < 5; i++ )
{
printf( "%d\n", rand() % ( high-low ) + ( low + 1 ) );
}
}
int main()
{
printRandomNumbers(0, 100);
}
If you don't want repeating numbers you're going to need to keep track of the output. Simplest way is using a basic c array[].

Patrick Borkowicz
- 1,206
- 10
- 19
-
http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful – Griwes Nov 11 '13 at 20:41
-
@Griwes Yes clearly there are better random number generators. However rand() is still taught in first-year C-programming classes for simplicity and the asker used it in his example code. – Patrick Borkowicz Nov 11 '13 at 20:53
-
The fact it's still taught (or the fact that "first year C programming" is a thing, IDK) is absurd. And since OP *used* rand, not asked for it, you can just tell him "your way is wrong, consider this one". Proper answer on SO (which is not really meant to solve homeworks) should not include `rand()`. Even in pre-11 C++, there's Boost.Random, so there's always a better way than `rand`. – Griwes Nov 11 '13 at 20:59