what i'm doing wrong in the array?

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[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";
	} 
	}

[/code]

Divide and conquer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <cstdlib>
#include <ctime>

bool is_present( const int array[], int before, int value )
{
    for( int i = 0 ; i < before ; ++i )
        if( array[i] == value ) return true ;

    return false ;
}

void generate_unique_randoms( int randoms[], int n, int max_digits )
{
    int mod_number = 10 ;
    while( max_digits-- > 1 ) mod_number *= 10 ;

    for( int i = 0 ; i< n ; ++i )
    {
        int candidate ;

        do candidate = std::rand() % mod_number ;
        while( is_present( randoms, i, candidate ) ) ;

        randoms[i] = candidate ;
    }
}

int main()
{
    std::srand( std::time(nullptr) ) ; // seed the lcg

    constexpr int MAXSZ = 100 ;
    int a[MAXSZ] ;

    std::cout << "number of random numbers to generate [1," << MAXSZ << "]:" ;
    int n ;
    std::cin >> n ;

    std::cout << "maximum number of digits each random number contains [1,6]: " ;
    int digits ;
    std::cin >> digits ;

    if( n > 0 && n <= MAXSZ && digits > 0 && digits < 6 )
    {

        generate_unique_randoms( a, n, digits ) ;

        for( int i = 0 ; i< n ; ++i ) std::cout << a[i] << ' ' ;
        std::cout << '\n' ;
    }
}
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( const int array[], int before, int value )
{
    for( int i = 0 ; i < before ; ++i )
        if( array[i] == value ) return true ;

    return false ;
}


Thanks
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.
You could use a vector if you want a dynamic array for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{
	int maxSize;
	int maxAmount;
	int ranNumber;
	vector<int> theNumbers;
	vector<int>::const_iterator numInter;

	srand(static_cast<unsigned int>(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( const int 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
made sense,Thanks!
Topic archived. No new replies allowed.