mardi 30 novembre 2021

How do I keep the random generated numbers from repeating inside the array?

I´m very new at coding and I want to create a programm that generates 6 random numbers in an array, and repeat the process 20 times. So far, I can get the array to fill with random numbers and to do the 20 loops but I can´t get the random numbers from repeating inside the array. Any tips?

Code:

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

int main()
{
    printf("\n5.1 Lottoziehung 6 aus 49 \n\n");

    int Ziehung[6]; 
    int Zufallszahl; //random variable
    srand((unsigned) time(0));
    for (int i=1; i<=20; i++)//Iterationen
    {
        printf("Ziehung %i: ",i); //Ziehungen Anzahl (the 20 loops)

                    for (int j=0;j<=5;j++){ //Array

                        int Zufallszahl=(rand() % 49)+1; //random num gen

                            if (Zufallszahl>0){
                            Ziehung[j]=Zufallszahl; //Array; Zahl innerhalb Ziehung widerholt
                            printf(" %i,",Ziehung[j]);
                            }//if
                            else {
                            j-1;
                            }//else
                }//for

                 printf("\n");
 }

    return 0;
}



Aucun commentaire:

Enregistrer un commentaire