Random generation of numbers - what's wrong with my code?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 13
Reputation: roachic is an unknown quantity at this point 
Solved Threads: 0
roachic roachic is offline Offline
Newbie Poster

Random generation of numbers - what's wrong with my code?

 
0
  #1
Apr 1st, 2008
Hi everyone. I'm trying to create an array of distinct random numbers. This is what I have so far.

  1. std::vector<int> randomNumberVector;
  2. randomNumberVector.resize(10);
  3.  
  4. void CBlah::Randomize(void)
  5. {
  6. int randomNumber;
  7. int length;
  8. int i;
  9. BOOL isTaken;
  10.  
  11. length = randomNumberVector.size();
  12.  
  13. for(i = 0; i<length;i++){
  14. while(randomNumberVector[i] == NULL){
  15. randomNumber = GenerateRandomNumber();
  16. isTaken = CheckRandomNumber(randomNumber);
  17.  
  18. if (isTaken == FALSE){
  19. randomNumberVector[i] = randomNumber;
  20. }
  21. }
  22. }
  23.  
  24. }
  25.  
  26. BOOL CBlah::CheckRandomNumber(int randomNumber)
  27. {
  28. int length;
  29. int j;
  30.  
  31. length = randomNumberVector.size();
  32.  
  33. for(j = 0; j<length;j++){
  34. if(randomNumber == randomNumberVector[j])
  35. {
  36. return TRUE;
  37.  
  38. }else{
  39.  
  40. return FALSE;
  41. }
  42. }
  43. }
  44.  
  45. int CBlah::GenerateRandomNumber(void)
  46. {
  47. int randomNumber;
  48.  
  49. srand ( time(NULL) );
  50.  
  51. randomNumber = rand() % 10;
  52.  
  53. return randomNumber;
  54. }

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..
Last edited by roachic; Apr 1st, 2008 at 7:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,512
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Random generation of numbers - what's wrong with my code?

 
0
  #2
Apr 1st, 2008
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 13
Reputation: roachic is an unknown quantity at this point 
Solved Threads: 0
roachic roachic is offline Offline
Newbie Poster

Re: Random generation of numbers - what's wrong with my code?

 
0
  #3
Apr 1st, 2008
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;
        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);

			}
		}
	}
	
}

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...
Last edited by roachic; Apr 1st, 2008 at 9:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,512
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Random generation of numbers - what's wrong with my code?

 
0
  #4
Apr 1st, 2008
post CheckRandomNumber() and GenerateRandomNumber() because the problem is in one of those two functions.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 13
Reputation: roachic is an unknown quantity at this point 
Solved Threads: 0
roachic roachic is offline Offline
Newbie Poster

Re: Random generation of numbers - what's wrong with my code?

 
0
  #5
Apr 2nd, 2008
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);

			}

                         if (isTaken == TRUE){

                                      MessageBox("taken");
                         }
		}
	}
	
}


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.
Last edited by roachic; Apr 2nd, 2008 at 1:16 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,512
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Random generation of numbers - what's wrong with my code?

 
0
  #6
Apr 2nd, 2008
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;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 13
Reputation: roachic is an unknown quantity at this point 
Solved Threads: 0
roachic roachic is offline Offline
Newbie Poster

Re: Random generation of numbers - what's wrong with my code?

 
0
  #7
Apr 2nd, 2008
Wow I don't think I'll ever notice that. Thanks a lot!! You rule
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,512
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Random generation of numbers - what's wrong with my code?

 
0
  #8
Apr 2nd, 2008
Originally Posted by roachic View Post
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
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 13
Reputation: roachic is an unknown quantity at this point 
Solved Threads: 0
roachic roachic is offline Offline
Newbie Poster

Re: Random generation of numbers - what's wrong with my code?

 
0
  #9
Apr 2nd, 2008
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC