How to generate 10 times random numbers without being repeated, with loop?

Asked

Viewed 98 times

0

#include<stdio.h>
#include<stdlib.h> 
#include<locale.h>
#include<conio.h>
#include<time.h>
#include<windows.h> 

int main(){

    int nSorteado[6];
    int i, j, n1, n2, op, seq[6];
    int Ncorretos=0, pontos=0, seg;
    
    srand(time(NULL));
    
    setlocale(LC_ALL, "Portuguese");
    system("color F4");
    
    for(i=0; i < 10; i++)
    {
        printf("############### JOGO DA MEMÓRIA #################");
        printf("\n*\n* Escolha um intervalo de números: ");
        scanf("%d %d", &n1, &n2);   
        printf("*\n");
        printf("* Embaralhando os números de %d a %d...\n*", n1, n2);
        
            for(i=0; i < 6; i++)
            {
                nSorteado[i] = n1 + rand() % (n2-n1+1);
            }
        
            printf("\n* Voce tem 5 segundos para decorar os 6 números.\n\n");
            for(i=0; i < 6; i++)
            {
                printf("%d ", nSorteado[i]);
            }
            seg=5;
            do
            {
                printf("\n\n%ds",seg);
                
                if(seg==20)
                {
                    seg=0;
                }
                Sleep(1000);
                seg--;         
            }while(seg>0);
            
            system("cls");
            
            printf("\nQual foi a sequencia dos 5 números sorteados?\n");
            printf("\n*Obs:. Digite os números com espaço entre eles. Ex: 1 2 3 4 5 6.\n\n");
            
            for(i=0; i < 6; i++)
            {
                scanf("%d", &seq[i]);
                if(seq[i] == nSorteado[i])
                {
                    Ncorretos++;
                }
            }
            
            if(Ncorretos == 6)
            {
                printf("Parabéns!! Você acertou!");
            }
            else
            {
                printf("Errado! Tente novamente...");
            }
            Sleep(1000);
            system("cls");
        }   
    return 0;
}

Note: Every time the code is executed, it generates 6 random numbers. The first time the code is executed and the user enters the correct sequence of the numbers, he receives a message of "Congratulations", but the other times that the user type the correct sequence, he receives an error message. I have tried several ways to tidy up but it did not work out the game work normally. If anyone can help me I really appreciate.

1 answer

-3

You can try to store the numbers in an array and whenever you generate, use one to check if the random number has already been drawn.

  • That kind of comment is no answer to the question, in which case, make it as a comment. Only ask answer when it actually solves the question itself.

  • I appreciate your reply. You could give an example of how to store random numbers in the array and how to check them with for?

Browser other questions tagged

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