Generate random number without repetition in C

2

Hello, I am making a memory game in c, and would like to know how to generate a random number without repetition. I will post what in the function so far. Do I need to do another function only to check if there is a repeated number?

void preencher_mesa(int matriz[4][4], int dificuldade)
{

    int i,j;
    int lim_col, lim_linha;



    for(i=0; i<4; i++)
        for(j=0;j<4;j++)
            matriz[i][j] = 0;


    if(dificuldade == 1)
    {
        lim_col = 3;
        lim_linha = 2;

    }
    else if(dificuldade == 2)
    {
        lim_col = 4;
        lim_linha = 2;
    }
    else if(dificuldade == 3)
    {
        lim_col = 4;
        lim_linha = 4;
    }

    srand(time(NULL) );
    for(i=0;i<lim_linha;i++)
    {
        for(j=0; j<lim_col;j++)
        {
                if(dificuldade == 1)
                {
                    matriz[i][j] = (rand()%3)+1;

                }
                else if(dificuldade == 2)
                {
                    matriz[i][j] = (rand()%6)+1;


                }
                else if (dificuldade == 3)
                {
                    matriz[i][j] = (rand()%8)+1;

                }



        }

    }

     mostrar_mesa(matriz);
}
    
asked by anonymous 21.06.2015 / 22:14

2 answers

1

You can find all possible numbers in an array; this array; then you use the required values.

int valores[] = {4, 5, 6, 7, 8, 9,
                 14, 15, 16, 17, 18, 19,
                 24, 25, 26, 27, 28, 29}; // 18 valores, para 16 posicoes
shuffle(valores); // ver, por exemplo, https://en.wikipedia.org/wiki/Fisher-Yates_shuffle

for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
        matrix[i][j] = valores[4*i + j];
    }
}

Edit

To fill the array according to the degree of difficulty you can do

for (i = 0; i < lim_col * lim_linha; i++) valores[i] = i / 2 + 1;

2nd edition

I made a program that does what you want.

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int randto(int n) {
    int r, rmax = (RAND_MAX / n) * n;
    do r = rand(); while (r >= rmax);
    return r % n;
}

void shuffle(int *data, int n) {
    while (n > 1) {
        int p = randto(n--);
        int tmp = data[n];
        data[n] = data[p];
        data[p] = tmp;
    }
}

int main(int argc, char **argv) {
    if (argc != 2) exit(EXIT_FAILURE);
    int carta[16];
    int base[] = {1, 2, 3, 4, 5, 6, 7, 8};
    int n;
    int dificuldade;
    srand(time(0));
    dificuldade = atoi(argv[1]);
    switch (dificuldade) {
        default: return 1; break;
        case 1: n = 3; break;
        case 2: n = 5; break;
        case 3: n = 8; break;
    }

    shuffle(base, sizeof base / sizeof *base);
    for (int i = 0; i < n; i++) carta[i + n] = carta[i] = base[i];
    shuffle(carta, 2 * n);

    printf("Seq:");
    for (int i = 0; i < 2 * n; i++) printf(" %d", carta[i]);
    printf("\n");

    return 0;
}

The output with different parameters was

% ./a.out 3
Seq: 2 7 8 3 6 1 4 1 5 5 7 6 8 3 4 2
% ./a.out 2
Seq: 1 5 7 1 5 8 4 4 7 8
% ./a.out 1
Seq: 5 2 6 6 2 5

    
21.06.2015 / 23:02
1

Use the srand((unsigned) time(NULL)) function, as exemplified below:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    const int MAX = 10;
    int i;
    srand((unsigned)time(NULL));
    for(i = 0; i < MAX; i++)
        printf("Elemento %d = %d\n", i, rand()%MAX);
    return 0;
}

With this code, 10 numbers will be printed on the screen and all are between 0 and 9 inclusive.

    
07.06.2017 / 15:23