# Writing unique numbers to an array

• 12-26-2006
yardy
Writing unique numbers to an array
Hi,
Am writing this program to write only unique random numbers to an array. The randomly generated numbers isnt the problem, its writing only unique numbers to the array thats a bother. Any ideas?

Code:

``` /*A program to generate random numbers and then write them to an array ONLY IF no such number exists in the array*/ #include <stdio.h> #include <stdlib.h> int GetRand(int min, int max); int const MAX=5; int main(void) {   int i, r, j,x;   int array[MAX];   for (i = 0; i < MAX; i++)   {           r = GetRand(0, 4); //generate random numbers btwn the 2 numbers indicated           printf ("the random number is %d\n", r);           if(i == 0 ) //writing first generated # to the array automatically for obvious reasons         {                 array[0]=r;                 printf("array[0]: %d\n\n",array[0]);/*this line works*/         }//end of if statement         if( i > 0 )//comparing from the 2nd # & upwards generated to whats in the array already         {                 for( j=1; j<MAX+1; j++ )                 {                                                 if(r == array[j-1])                         {                                 i=i-1;                                 break;                         }                         else x=1;                 }                                 if( x = 1 )                 array[j]=r;                                         }//end of if statement   }//end of for loop             return(0); } int GetRand(int min, int max) //function to generate random numbers {         static int Init = 0;         int rc;                 if (Init == 0)         {                 srand(time(NULL));         Init = 1;         }                         rc = (rand() % (max - min + 1) + min);                 return (rc); }//end of GetRand function```
• 12-26-2006
pprabhakar
below 'for' loop is the culprit

Code:

``` if( i > 0 )//comparing from the 2nd # & upwards {     for( j=1; j<MAX+1; j++ )```
• 12-26-2006
Happy_Reaper
Make an array of n numbers containing the numbers 1 to n. Then, from there, randomly shuffle that array. Then take the amount of number you want from there.
• 12-26-2006
yardy
What exactly is wrong with the for loop? Or is it in the wrong place?
• 12-26-2006
Happy_Reaper
If j is equal to max+1, array[j] will cause a seg fault. But you should stick to what I told you and search the boards. This question has been answered a million times already.
• 12-27-2006
ssharish2005
Code:

```if( x == 1 )     array[j]=r;```
looks like u have over complicated the code. The problem is just straight forward, follow Happy_Reaper post.

ssharish2005
• 12-27-2006
yardy
Thanks guys. I got everything working. The completed code might help somebody else:

Code:

``` /*A program to generate random numbers and then write them to an array ONLY IF no such number already exists in the array*/ #include <stdio.h> #include <stdlib.h> int GetRand(int min, int max); int const MAX = 10; int i; int numAlreadyFound = 0; int r; int main() {         int unique[MAX];         //the first r is automatically entered into unique[0];         r = GetRand(0, 9);         unique[0] = r;         numAlreadyFound++;                         //as long as total number is less than MAX         while(numAlreadyFound < MAX)         {         //generate new r         r = GetRand(0, 9);                 //check each value in array against current r value         for(i = 0; i < numAlreadyFound ; ++i)         {         //if current r already exists in the array         if(unique[i] == r)         //stop looking at values in array         i = 1000;         }                 //if current r not found in array         if(i == numAlreadyFound)         //add it to the end of the values already found and increment         //numAlreadyFound         unique[numAlreadyFound++] = r;         }                 //print the elements of the array         for( i = 0;i < MAX; i++ )         printf("elements of unique are:%d\n",unique[i]); } int GetRand(int min, int max) //function to generate random numbers {         static int Init = 0;         int rc;                 if (Init == 0)         {                 srand(time(NULL));         Init = 1;         }                         rc = (rand() % (max - min + 1) + min);                 return (rc); }//end of GetRand function```