Question: Unique Random Integers ( set ) Given integer inputs howMany and maxNum, generate a vector of howMany unique random integers from 0 ( inclusive )

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:
58
the output is
54016[2 retries]
Use this as a template for your file, a2.5.cpp:
#include
#include
#include
using namespace std;
void PrintNums(vector nums, int size);
vector UniqueRandomInts(unsigned int howMany, int maxNum, int& retries);
int main(){
int howMany;
int maxNum;
int retries;
cin >> howMany;
cin >> maxNum;
vector 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(vector 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 UniqueRandomInts(unsigned int howMany, int maxNum, int& retries){
int nextRand;
retries =0;
set alreadySeen;
vector nums;
/* Type your code here. */
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!