throw out used rand numbers

Relatively new to c++. I have written a game that generates a random number, which it does. However every now and then it will repeat a number. Is there a way to throw out used numbers to avoid repeats?
I will post my code if anyone sees it necessary. Thanks.

When ever this comes up, I suggest that people pre-populate an array with the unique values in the range that you want. Then you achieve your randomness by using std::random_shuffle against that array. Now, instead of checking against a growing queue of numbers for every generation, you simply pop a value of the top of your now shuffled deck. It's not the traditional method, but it looks a hell of a lot cleaner.
Are you saying to use command random_shuffle instead of rand()? right now my random statement is rand() % 10 +1 and I use the seed srand (time(0)); I'm not sure if you're meaning that's a seed or command?
The function "std::random_shuffle()" is not an outright replacement for "srand()" and "rand()", which are deprecated by the way.

Here is some info on it: http://en.cppreference.com/w/cpp/algorithm/random_shuffle

As you can see this function does not generate the numbers, it only shuffles the elements of already existing arrays.
There is randomness and there is randomness. Same number occurring more than once is random. Lets take the simplest example:

Toss a coin.

You can get heads or you can get tails. Would it be really "random" to enforce that the second toss must result in different value than the first toss? How long could you generate numbers that way, if you can toss only two times (since there are only two unique values)?

Nevertheless, that seems to be what luckyfor3v3r needs. You take one of each possible value, shuffle the lot once, and then draw them in the order that they are after the shuffle.


With heads and tails that is a short game. With a deck of 52 unique cards the game is known to last a bit longer. Serious gamblers will go through multiple decks though, so they can see same "numbers" again after a while (but not on same deal).
Topic archived. No new replies allowed.