#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
// nValues is the number of unique values to generate.
// vals points to an array large enough to hold nValues numbers.
// min is the smallest number in the range to generate numbers for.
// max is the largest number in the range to generate numbers for.
// IMPORTANT: max-min cannot exceed 255.
void generateUniqueRandomValues(unsigned nValues, unsigned* vals, unsigned min, unsigned max)
{
using std::swap;
using std::rand;
const unsigned maxElements = 256 ; // some arbitrary value.
unsigned values[maxElements];
const unsigned valuesSize = max - min + 1; // the number of elements of the values array which we will use.
for (unsigned i = 0; i < valuesSize; ++i) // fill values with numbers from min to max.
values[i] = min + i;
for (unsigned i = 0; i < nValues; ++i)
{
const unsigned valuesLeft = valuesSize - i;
const unsigned index = rand() % valuesLeft; // pick a random index for an unused value.
vals[i] = values[index]; // store the value in the user-supplied array.
swap(values[index], values[valuesLeft-1]); // swap the used value with the last unused value.
}
}
int main()
{
using namespace std;
srand(time(0));
const unsigned nVals = 7;
const unsigned minVal = 1;
const unsigned maxVal = 50;
unsigned randValues[nVals];
generateUniqueRandomValues(nVals, randValues, minVal, maxVal);
for (unsigned i = 0; i < nVals; ++i)
cout << "#" << i+1 << ": " << randValues[i] << '\n' ;
}