I'm building a simulation of a reality game and I need to randomly pick the indexes of the players which will take part in every round of a competition. The problem is, that while on the first round the indexes are sometimes the same for both teams and sometimes not, on the second and third round there are always the same indexes in both teams. Could anyone shed some light on this?
Code follows:
#include <iostream>
using namespace std;
int ArraySearch(int* a, int num);
int compete()
{
int round = 0; //current round
int temp = 0; //(temporary) random player index
int Team1Players[5]; //player indexes of team 1
int Team2Players[5]; //indexes of 2
round++; //start from the 1st round
while (round < 4) //competition lasts for 3 rounds
{
srand(time(NULL));
cout << "This is Round " << round << endl; //DEBUG MSG
cout << "Team 1 Players: "; //DEBUG MSG
for (int i = 0; i < 5; i++)
{
do //change the chosen number when it already exists
{
temp = rand() % 11;
Team1Players[i] = temp;
//cout << "Grabbed a random number for team 1: " << Team1Players[i] << endl; //DEBUG MSG
}
while (ArraySearch(Team1Players, temp) == 1);
cout << Team1Players[i] << " "; //DEBUG MSG
}
cout << endl;
cout << "Team 2 Players: ";
for (int i = 0; i < 5; i++)
{
do //same as above for team 2
{
temp = rand() % 11;
Team2Players[i] = temp;
//cout << "Grabbed a random number for team 2: " << Team2Players[i] << endl; //DEBUG MSG
}
while (ArraySearch(Team2Players, temp) == 1);
cout << Team2Players[i] << " "; //DEBUG MSG
}
cout << endl;
round++;
}
}
int ArraySearch(int* a, int num) //returns 1 when a number exists more than once, 0 when it does not exist or is unique
{
int occurrences = 0;
for (int i = 0; i < 5; i++)
{
if (a[i] == num)
{
occurrences += 1;
}
}
if (occurrences > 1)
{
return 1;
}
return 0;
}
Many thanks in advance.
Aucun commentaire:
Enregistrer un commentaire