Hi everyone. I'm trying to create an array of distinct random numbers. This is what I have so far.
std::vector<int> randomNumberVector;
randomNumberVector.resize(10);
void CBlah::Randomize(void)
{
int randomNumber;
int length;
int i;
BOOL isTaken;
length = randomNumberVector.size();
for(i = 0; i<length;i++){
while(randomNumberVector[i] == NULL){
randomNumber = GenerateRandomNumber();
isTaken = CheckRandomNumber(randomNumber);
if (isTaken == FALSE){
randomNumberVector[i] = randomNumber;
}
}
}
}
BOOL CBlah::CheckRandomNumber(int randomNumber)
{
int length;
int j;
length = randomNumberVector.size();
for(j = 0; j<length;j++){
if(randomNumber == randomNumberVector[j])
{
return TRUE;
}else{
return FALSE;
}
}
}
int CBlah::GenerateRandomNumber(void)
{
int randomNumber;
srand ( time(NULL) );
randomNumber = rand() % 10;
return randomNumber;
}
What I am doing in the Randomize method is checking if the current element of a vector is empty, if it's empty, get a random number, and check if it's been added. I'm not getting the proper results, when I get to the second i, for example it's 2, the number 2 will be added to the rest of the vector. Can anyone spot my mistake? Thanks a lot!
Oh btw, can someone also explain what this is 'srand ( time(NULL) );'. I read that I have to use it for some reason but I'm not too sure..