hello

How do I ensure that the same number does not appear more than once in my random number creator?

Eg of output currently:

array = {30, 2, 12, 9, 2}; // has repeats

Eg of what I am trying to do:

array = {30, 2, 12, 9, 7}; // no repeated numbers

void generate_post::random(int array[], int max) {

	srand(time(NULL));

	for (int i = 0; i < 6; i++)
	{
		array[i] = (rand()%max)+1;
	}
}

Store the random number in a variable. Then search the array for the randomly generated number. If it is found, generate another random number. Repeat until random number does not exist in the array.

Create an array;
From 'i' to 'SIZE' store 'i' into array.
shuffle array;

extract info from array into new one, without the same index
repeating.

commented: nice solution +2

Create an array;
From 'i' to 'SIZE' store 'i' into array.
shuffle array;

extract info from array into new one, without the same index
repeating.

Would you be able to show me g=how to do this in code, I'm a bit confused :P

Would this work?

void generate_post::random(int array[], int max) {

	int n;
	srand(time(NULL));

	for (int i = 0; i < 6; i++)
	{
		n = (rand()%max)+1;
		for (int j=0; j<=i; j++) {
			if (array[j] != n)
				array[i] = n;
		}
	}
}

something like this(not tested) :

void genRan(int array[], int sz)
{ 
   int *tmp = new int[sz];
   for(int i = 0; i < sz; i++)
         tmp[i] = i;

    //pick randome index from tmp, check if its a unique index
    // if so then populate array with the content from tmp.
  
   delete [] tmp;
}

You aren't keeping a tally, only existance of a number so how about using a bit array.

65,536 numbers 0x10000 range from 0 to 65,535.
So technically you only need one bit per number. So let's assume you have 32-bit integers. Then 0x10000 / 0x0020 = 2048 slots, 32-bits each.
So

#define MAX_BIT  2048
 unsigned int bary[ MAX_BIT ];

void ClrAry( void )
{
   for (int i = 0; i < MAX_BIT; i++ )
        bary[i] = 0;
}

void SetBit( unsigned int nBit )
{
    bary[ nBit >> 5 ] |= 1 << (nBit & 0x1f);
}

bool GetBit( unsigned int nBit )
{
   unsigned int n;
    n = bary[ nBit >> 5 ];
    return (bool)(( n >> (nBit & 0x1f)) & 1);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.