0
I am creating a vector with random numbers from 1 to 60 using the Rand function. This vector cannot contain repeated numbers. For example, if srand’s return was number 3, and there is already a number 3 in the vector, I need to generate another random number and redo the check. But I’m not sure how to do that repetition.
For now I’ve done it:
int vetor[30]={0}; //preciso de 30 valores aleatorios diferentes, aqui o vetor está cheio com zeros
candidato = 1+rand()%59; //variavel que armazenará o valor a ser testado
Set up an array with 60 numbers from 1 to 60, shuffle this array and take the first 30 numbers, copying them to another array. The shuffling algorithm is the Fisher-Yates.
– Victor Stafusa
Perfect! Much more optimized than creating auxiliary vectors or scrolling through to check every time. Thanks @Victorstafusa
– Bipe