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?
int modNumber = 0;
int randomNumber = 0;
int j = 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)
if ( randoms[j] >=10)
cout<< randoms[j]<< "\t";
> 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.
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