i'm working in this school project for more than two weeks but i don't see any light yet. I read several post in this forum but other than make it clear confuse me more. Im trying to remove the repeated random number from my array and replace with a new random number but i don't get it. What i'm doing wrong?
[code]
void TargetGen::generateRandoms()
{
srand(time(0));
int modNumber = 0;
int randomNumber = 0;
int j = 0;
int temp=0;
userMax= 0;
digitMax=0;
cout << "Enter the maximum number of random numbers to generate: ";
cin >> userMax;
cout << "Enter the maximum number of digits each random numbers contains: ";
cin >> digitMax;
if (digitMax == 2)
{
modNumber = 100;
}
for ( int i = 0; i< userMax; i ++)
{
randoms[j]=rand() % modNumber;
for (int k = 1; k < randoms[j]; k++)
{
bool repeat = false;
for (int j = 0; (j < k) && (repeat == false); j++)
if (randoms[j] == randoms[k]) repeat = true;
if (!repeat) ;
}
if ( randoms[j] <=10)
{
i--;
}
if ( randoms[j] >=10)
{
cout<< randoms[j]<< "\t";
}
}
The problem in your approach is that if you replace a duplicate with something new, you have to again check that it is not a duplicate.
I would recommend using std::set in the first place so that no duplicates needs to be removed, but set has its own features that defy randomness. As a helper, perhaps ...
I believe that the function below is checking for the duplicate value but how. I think this is what i need to understand: How this function knows what number are duplicate in the array?
1 2 3 4 5 6 7 8
// "JLBorges line 5 - 11"
bool is_present( constint array[], int before, int value )
{
for( int i = 0 ; i < before ; ++i )
if( array[i] == value ) returntrue ;
returnfalse ;
}
It doesn't. It does check whether an array already contains a specific value. Where is it used, other things will ensure that a duplicate is never added to the array.
{
int maxSize;
int maxAmount;
int ranNumber;
vector<int> theNumbers;
vector<int>::const_iterator numInter;
srand(static_cast<unsignedint>(time(0)));
cout << "Enter a max amount of numbers to generate:" << endl;
cin >> maxAmount;
cout << "\nEnter a max size of the numbers to generate:" << endl;
cin >> maxSize;
int count = 0;
while(count != maxAmount)
{
if (maxSize < maxAmount)
{
cout << "Error cannot generate enough unique numbers with user chosen settings." << endl;
return 1;
}
ranNumber = rand();
ranNumber = ranNumber % maxSize;
numInter = find(theNumbers.begin(), theNumbers.end(), ranNumber);
if (numInter == theNumbers.end())
{
theNumbers.push_back(ranNumber);
++count;
}
}
cout << "\nNumbers Generated:\n\n";
for (numInter = theNumbers.begin(); numInter != theNumbers.end(); ++numInter)
cout << *numInter << endl;
cout << "\nThere are " << theNumbers.size() << " numbers generated." << endl;
return 0;
}
Though rand will be limited to 32767 so you may want to put in some protection to stop the user trying to generate more numbers then that otherwise you will be stuck in a endless loop.
> How this function knows what number are duplicate in the array?
> bool is_present( constint array[], int before, int value )
This function checks if value is present in the array somewhere prior to position before.
If it finds the element, it returns true; otherwise it returns false.
This
1 2 3 4
int candidate ;
do candidate = std::rand() % mod_number ;
while( is_present( randoms, i, candidate ) ) ;
a. generates a candidate random number, to be placed into the array at position i
b. uses is_present() to check if the candidate number is already present in the array, before position i
c. if it is already present, it discards the duplicate and goes back to step a. to generate another candidate.
d. If not, the candidate is not a duplicate, and it places it into the array at position i