I am trying to generate a short range of (about 20) different / unique random numbers.
Here is what I have now:
unique_random.h
:
#ifndef UNIQUE_RANDOM_H
#define UNIQUE_RANDOM_H
// generates a pseudo-random number in [min, max]
int random_number (unsigned int min, unsigned int max) {
static bool seed_initialized = false;
if (!seed_initialized) {
seed_initialized = true;
srand((unsigned int) time(NULL));
}
return rand() % (max - min + 1) + min;
}
// generates a random number different from the previously generated
int random_number_without_these (int min, int max, std::set<int>& generated) {
int res = random_number (min, max);
// if res one of the previous, generate again
while (s.find(res) != s.end()) {
res = random_number (min, max);
}
return res;
}
#endif
then the above functions would be called like so:
main.cpp
:
#include <iostream>
#include <time.h>
#include <set>
#include "unique_random.h"
int main() {
std::set<int> already_generated;
for (auto i = 0; i < 20; ++i) {
int rand = random_number_without_these(1,20, already_generated);
already_generated.insert(rand);
}
}
where the expected result is that there have been generated 20 consecutively unique values. What I have written now needs two functions, random_number_without_these()
, random_number()
and a container, set<int>
, in order to work, that is why I am wondering:
Is there an easier way to generate short range of unique random numbers, possibly along the lines of the existing code?