Home > Software design >  How to randomize a to p without repitition
How to randomize a to p without repitition

Time:01-09

I want to randomize a to p without repetition.

int main(){
    int array2[4][4];
    bool arr[100]={0};
    int i;
    int j;

    srand(time(NULL));
    for(i=0; i<=3; i  ){

        for(j=0; j<=3; j  ){
            int randomNumber1;
            randomNumber1 = (rand() % (82-65 1)) 65;
        
            if (!arr[randomNumber1])
            {
                printf("%c ",randomNumber1);
                array2[i][j]=randomNumber1;
            }
            else
            {
                i--;
                j--;
                arr[randomNumber1]=1;
            }
        }
        printf("\n");
    }

    return;

the output still has repeat alphabet. I want to have the output in 4x4 with with all a to p without it repeating.

CodePudding user response:

There are some errors in your code. IMHO the most serious is that arr[randomNumber1]=1; is is the wrong branch of the test. That means that your current code does not invalidate once a number was used but only if it has already been invalidated => if you control the arr array at the end of the program all value are still 0.

That is not all. When you get a duplicate, you should only reset the inner loop, and you are currently off by 2 in your maximum ascii code: you go up to R when you want to stop at P.

Your code should be:

for (i = 0; i <= 3; i  ) {

    for (j = 0; j <= 3; j  ) {
        int randomNumber1;
        randomNumber1 = (rand() % (81 - 65))   65;

        if (!arr[randomNumber1])
        {
            printf("%c ", randomNumber1);
            array2[i][j] = randomNumber1;
            arr[randomNumber1] = 1;
        }
        else
        {
            //i--;
            j--;
        }
    }
    printf("\n");
}

But this kind of code is terribly inefficient. In my tests it took 30 to 60 steps to fill 16 values, because random can return duplicates. This is the reason why you were advised in comments to use instead the modern algorithm for Fisher-Yates shuffle:

int main() {
    int array2[16];
    unsigned i, j, k=0;

    // initialize array with alphabets from A to P
    for (i = 0; i < sizeof(array2); i  ) {
        array2[i] = 'A'   i;
    }
    // Use Fisher-Yates shuffle on the array
    srand(time(NULL));
    for (i = 15; i > 0; i--) {
        j = rand() % (i   1);
        if (j != i) {
            int c = array2[i];
            array2[i] = array2[j];
            array2[j] = c;
        }
    }
    // Display a 4x4 pattern
    for (i = 0; i < 4; i  ) {
        for (j = 0; j < 4; j  ) {
            printf("%c ", array2[k  ]);
        }
        printf("\n");
    }
    return 0;
}

Which shuffles the array in only 16 steps.

CodePudding user response:

Here is the outline

// Need some #includes here - exercise for the reader
char items[] = "abcdefghijklmnopqrstuvwxyz";
int len = sizeof(items);
srand(time(NULL));

while (len > 0) {
   int r = rand() % len;
   printf("%c", items[r]);
   len--;
   items[r] = items[len];
}

This should do the trick to print the whole alphabet in random order without repeats. Modify to do what you need it to do

  •  Tags:  
  • Related