Hello guys, I am new to c++ and I was trying to make a function which generates non repeated numbers.
Would u guys mind helping me have a look on my code?
here is my function:
You are not clear on the specs here. Let's assume you want n non-repeating numbers from 0 to N-1. Generate a vector consisting of v = {0,1,2,...,N-1} (std::iota could be used if you want). Then shuffle v using std::shuffle or std::random_shuffle. Then extract the first n elements of the resulting vector.
If N is very large, then instead of shuffling v, then carry out a loop wherein constint r = std::rand() % v.size();
store v[r] wherever you want your generated numbers to be stored, then remove it from the vector using the erase-remove idiom std::erase (std::remove(v.begin(), v.end(), v[r]), v.end());
and then repeat until you've done this n times. If v is not consecutive numbers, then construct v however you want (it doesn't even have to be a vector of integers) and do the exact same thing.