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..

>>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

srand() seeds the random number generator so that it will generate a different set of random numbers every time you run the program, providing you pass a different number to srand() each time. Most people just pass the return value from time() function because it is different everyt time you run the program. srand() should be called only once during the lifetime of the program, so you need to move it from GenerateRandomNumber() to near the beginning of main().

CheckRandomNumber() is incorrect. delete else return FALSE in that loop because it causes the loop to terminate after the first iteration. Just place a return FALSE; as the last line of that function.

Thank you for the reply. I did as you said but when I ran the randomize method on Initdialog, nothing happens to my program. My CDialog doesn't show up but my program is still running, with no errors or anything. It's like it's busy with something else. Could the way I'm randomizing the numbers take up a lot of time?

Edit: I did this:

void CBlah::Randomize(void)
{
	int randomNumber;
	int length;
	int i;
	BOOL isTaken;
        [B]CString b;[/B]

	length = randomNumberVector.size();
	
	for(i = 0; i<length;i++){
		while(randomNumberVector[i] == NULL){
			randomNumber = GenerateRandomNumber();
			isTaken = CheckRandomNumber(randomNumber);

			if (isTaken == FALSE){
				randomNumberVector[i] = randomNumber;
                                 
                              [B]  b.Format("%d",randomNumberVector[i]);
				MessageBox(b);[/B]

			}
		}
	}
	
}

And it prints out all the distinct ints...but when I put a MessageBox after the Randomize() method is called, that MessageBox doesn't appear. Why is it that it's not getting past my randomize method?? Could it be a problem with my loop? This is very puzzling...

post CheckRandomNumber() and GenerateRandomNumber() because the problem is in one of those two functions.

Here it is...

void CBlah::Randomize(void)
{
	int randomNumber;
	int length;
	int i;
	BOOL isTaken;
        CString b;

	length = randomNumberVector.size();
	
	for(i = 0; i<length;i++){
		while(randomNumberVector[i] == NULL){
			randomNumber = GenerateRandomNumber();
			isTaken = CheckRandomNumber(randomNumber);

			if (isTaken == FALSE){
				randomNumberVector[i] = randomNumber;
                                 
                                b.Format("%d",randomNumberVector[i]);
				MessageBox(b);

			}

                         [B]if (isTaken == TRUE){

                                      MessageBox("taken");
                         }[/B]
		}
	}
	
}


BOOL CBlah::CheckRandomNumber(int randomNumber)
{
	int length;
	int j;
	
	length = randomNumberVector.size();

	 for(j = 0; j<length;j++){
			if(randomNumber == randomNumberVector[j])
			{
				return TRUE;

			}
				
	 }

         return FALSE;
}

int CBlah::GenerateRandomNumber(void)
{
	 int randomNumber;

	 //srand ( time(NULL) ); <--moved to oninitdialog

	 randomNumber = rand() % 10;
	
	 return randomNumber;
}

I'm not sure if it's any use....but I added the bolded part, and I found that after it adds the certain number of ints, 'Taken' keeps appearing over and over again.

The first time GenerateRandomNumber() returns 0 causes an infinite loop. Make it return a value from 1-10 instead of 0-9 by adding 1 to the result. randomNumber = rand() % 10 + 1;

Wow I don't think I'll ever notice that. Thanks a lot!! You rule :)

Wow I don't think I'll ever notice that. Thanks a lot!! You rule :)

You can easily find such error yourself by learning how to use your compiler's debugger :)

Actually, thinking about it, I'm not that sure why returning a 0 causes an infinite loop. I know this sounds like a stupid question. So if I wanted an array of 0 - 9, is it not possible because of the 0 it returns?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.