| | |
Random generation of numbers - what's wrong with my code?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 13
Reputation:
Solved Threads: 0
Hi everyone. I'm trying to create an array of distinct random numbers. This is what I have so far.
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..
C++ Syntax (Toggle Plain Text)
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..
Last edited by roachic; Apr 1st, 2008 at 7:42 pm.
>>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
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.
•
•
Join Date: Mar 2008
Posts: 13
Reputation:
Solved Threads: 0
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:
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...
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.
•
•
Join Date: Mar 2008
Posts: 13
Reputation:
Solved Threads: 0
Here it is...
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.
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.
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.
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.
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Simple Random Number Program Help (C++)
Other Threads in the C++ Forum
- Previous Thread: c++: exercuting a program with two files
- Next Thread: Visual Representation of Lander
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






