-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?
– Maniero
cool there was only one example, the number of values has to vary up to 0 until 7
– rafael marques
but you have to clarify better otherwise there is no help.
– Maniero
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
– rafael marques
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?
– Maniero
in fact is almost right, each number can only repeat 2 times
– rafael marques
You know what’s random? It’s a beautiful day you wake up Michael Stevens
– Jefferson Quesado