How to generate random value pairs

-1

I wanted to generate 6 random values in 2 pairs for example 1 1, 2 2, 3 3, but I'm not getting my code

My code

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

int vezes(int vetor[], int tamanho, int numero);

int main(int argc, char** argv)
{
  int vetor[6], num, resu;
  srand(time(NULL));
  for(int i = 0; i < 6; i++)
  {
     num = rand() % 8;
     if(i != 0)
     {
        resu = vezes(vetor, i, num);
        while(resu)
        {
            num = rand() % 8;
            resu = vezes(vetor, i, num);
        }
     }
     vetor[i] = num;
  }
  for(int i = 0; i < 6; i++)
  {
     printf("%d ", vetor[i]);
  }
   return 0;
}

int vezes(int vetor[], int tamanho, int numero)
{
  int cont = 0;
  for(int i = 0; i < tamanho; i++)
  {
     if(vetor[i] == numero)
     {
        cont++;
     }
 }
  if(cont <= 2)
  {
    return 1;
  }
  return 0;

 }
    
asked by anonymous 16.07.2018 / 01:08

1 answer

1

I do not know if it was for you that I said, you have to understand what you are doing, you are asking several questions here and it shows that you do not understand anything that is happening in the code, you are not learning this way. Your code is still poorly written, which does not require much knowledge. He continues to err in things that have already been taught. I'm not even talking about logic.

One problem is not clearly understanding what the problem is and communicating what it is. This is a problem before programming. You need to work on this to get it done.

The logic is too confusing, and it is because you do not understand the problem. It's much simpler. Even performance is better. And you can probably improve it more.

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

void shuffle(int *array, int tamanho) { //sorteia garantindamente único - algoritmo Fisher-Yates
    for (int i = tamanho - 1; i > 0; i--) {
        int j = rand() % (i + 1);
        int tmp = array[j];
        array[j] = array[i];
        array[i] = tmp;
    }
}

int main() {
    srand(time(NULL));
    int roll[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; //cria os valore permitidos
    shuffle(roll, 8); //embaralha
    int resultado[6];
    for (int i = 0; i < 3; i++) { //só escolhe 3 números conforme a definição
        resultado[i] = roll[i];
        resultado[i + 3] = roll[i]; //repete o número
    }
    shuffle(resultado, 6); //embaralha os números que estavam agrupados
    for (int i = 0; i < 6; i++) printf("%d ", resultado[i]);
}

See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

    
16.07.2018 / 01:39