Preventing the same Random Number from generating

I am trying to write a program that uses a random number to pick a monster object pointer from an array and place it into an allied monster array. (i am basically assigning random monster teams without repeated monsters on either team.) This is my code for this section. Let me know if you want more code or if there is a problem with my logic (which is what i think is wrong)

1
2
3
4
5
6
7
8
9
10
11
12
13
  for(int i = 0; i < teamSize; i++)
    {
        randomMonsterNum = randomNumberGenerator();
        int currentNum = 0;
        while (currentNum == randomMonsterNum)
        {
            randomMonsterNum = randomNumberGenerator();
            //allyMonsterArr[i] = MonsterPtrArr[randomMonsterNum];
        }
        allyMonsterArr[i] = MonsterPtrArr[randomMonsterNum];
        currentNum = randomMonsterNum;
    }
Last edited on
Your problem is similar to card games:

Take a deck of cards (a list of different monsters).
Shuffle the "deck".
Draw as many cards as you do need (but no more than is in the deck).

http://www.cplusplus.com/reference/numeric/iota/
http://www.cplusplus.com/reference/algorithm/shuffle/
Topic archived. No new replies allowed.