How to generate random value pairs

Asked

Viewed 272 times

-1

I wanted to generate 6 random values in 2 pairs for example 1 1, 2 2, 3 3

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;

 }
  • I don’t understand what you want. You want 3 random numbers and it appears repeated, is that it? And it goes as far as one?

  • cool there was only one example, the number of values has to vary up to 0 until 7

  • but you have to clarify better otherwise there is no help.

  • I’ll give you a better example 1, 2, 3, 1, 2 ,3, in case there are two 1, two 2, two 3, gave you understand

  • So that’s what I said, you want three possibly different numbers that can go from 0 to 7, and then each of them must appear again? Is that it? Could be 1 2 1 2 1?

  • in fact is almost right, each number can only repeat 2 times

  • You know what’s random? It’s a beautiful day you wake up Michael Stevens

Show 2 more comments

1 answer

3


I don’t know if it was you I was talking to, you need to understand what you’re doing, you’re already asking a lot of questions here and you show that you don’t understand anything that’s going on in the code, you’re not learning it that way. His code is still poorly written, which is something that does not even require much knowledge. It continues to err on things that have already been taught. I’m not even talking logic.

One problem is not clearly understanding what the problem is and communicating what it is. It is a problem prior to programming. needs to work this to be able to program.

The logic is too confusing, and it’s because it doesn’t understand the problem. It’s much simpler. Even the performance is better. And you can probably get better.

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

void shuffle(int *array, int tamanho) { //sorteia garantidamente ú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]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.