Project 27.5: Unique Random Integers (set)
Given integer inputs howMany and maxNum, generate a vector of howMany unique random integers from 0 (inclusive) to maxNum (exclusive).
The structure of the program is:
- main() calls UniqueRandomInts() with arguments howMany, and maxNum.
- UniqueRandomInts() returns a vector of howMany unique random integers.
- The required output is already provided in main() and PrintNums().
Complete UniqueRandomInts(), which generates random integers until howMany unique integers have been collected in vector nums.
Hint: If a generated number is new, add the number to vector nums and set alreadySeen. If the number has been seen before, increment the global variable retries and generate another random integer.
Note: For testing purposes, the random number generator is seeded with a fixed value (641) in main().
Ex: When the input is:
5 8
the output is
5 4 0 1 6 [2 retries]
(Note that your output may not match when you run this on your system, because your system may generate different random numbers. However, if your code is correct, the output will match when you submit to zyBooks.)
Use this as a template for your file. Full documentation is required.
#include <iostream>
#include <vector>
#include <set>
using namespace std;
void PrintNums(const vector<int>& nums, int size);
vector<int> UniqueRandomInts(unsigned int howMany, int maxNum, int& retries);
int main() {
int howMany;
int maxNum;
int retries;
cin >> howMany;
cin >> maxNum;
vector<int> uniqueInts;
srand(641); // Seed random number generator for testing
uniqueInts = UniqueRandomInts(howMany, maxNum, retries);
PrintNums(uniqueInts, howMany);
cout << " [" << retries << " retries]" << endl;
}
// Print the integers in vector nums separated by a space
void PrintNums(const vector<int>& nums, int size) {
for (int i = 0; i < size; ++i) {
cout << nums.at(i) << " ";
}
}
// Generate howMany unique random integers 0 <= N < maxNum and return in nums
vector<int> UniqueRandomInts(unsigned int howMany, int maxNum, int& retries) {
int nextRand;
retries = 0;
set<int> alreadySeen;
vector<int> nums;
/* Type your code here. */
}