#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define yy 6
#define xx 1000
#define zz 49
int *main_Lotto;
int **date_Lotto;
int *Temp;
int **double_Temp;
void Sort(int* tab, int w)
{
int i,j,k;
for(i=1;i<w;i++)
{
j=i;
k=tab[j];
while((j>0) && (tab[j-1]>k))
{
tab[j]=tab[j-1];
j--;
}
tab[j]=k;
}
}
int is_Zero(int *tab1,int *tab2, int w)
{
int i,sum;
sum=0;
for(i=0;i<w;i++)
{
if(tab1[i]==tab2[i])
++sum;
}
if(sum==0)
return 1;
else
return 0;
}
int is_One(int *tab1,int *tab2, int w)
{
int i,sum;
sum=0;
for(i=0;i<w;i++)
{
if(tab1[i]==tab2[i])
++sum;
}
if(sum==1)
return 1;
else
return 0;
}
int is_Two(int *tab1,int *tab2, int w)
{
int i,sum;
sum=0;
for(i=0;i<w;i++)
{
if(tab1[i]==tab2[i])
++sum;
}
if(sum==2)
return 1;
else
return 0;
}
int is_Three(int *tab1,int *tab2, int w)
{
int i,sum;
sum=0;
for(i=0;i<w;i++)
{
if(tab1[i]==tab2[i])
++sum;
}
if(sum==3)
return 1;
else
return 0;
}
int is_Four(int *tab1,int *tab2, int w)
{
int i,sum;
sum=0;
for(i=0;i<w;i++)
{
if(tab1[i]==tab2[i])
++sum;
}
if(sum==4)
return 1;
else
return 0;
}
int is_Five(int *tab1,int *tab2, int w)
{
int i,sum;
sum=0;
for(i=0;i<w;i++)
{
if(tab1[i]==tab2[i])
++sum;
}
if(sum==5)
return 1;
else
return 0;
}
int is_Six(int *tab1,int *tab2, int w)
{
int i,sum;
sum=0;
for(i=0;i<w;i++)
{
if(tab1[i]==tab2[i])
++sum;
}
if(sum==6)
return 1;
else
return 0;
}
int main(int argc, char **argv)
{
int i,j,k;
int x0,x1,x2,x3,x4,x5,x6;
srand(time(NULL));
x0=x1=x2=x3=x4=x5=x6=0;
main_Lotto=malloc(yy*sizeof(int));
Temp=malloc(zz*sizeof(int));
date_Lotto=malloc(xx*sizeof(int*));
double_Temp=malloc(xx*sizeof(int*));
for(i=0;i<xx;i++)
{
date_Lotto[i]=malloc(yy*sizeof(int));
double_Temp[i]=malloc(zz*sizeof(int));
}
for(i=0;i<zz;i++)
Temp[i]=1;
for(i=0;i<xx;i++)
for(j=0;j<zz;j++)
double_Temp[i][j]=1;
i=0;
do
{
do
{
j=rand()%zz;
}while(Temp[j]!=1);
Temp[j]=0;
main_Lotto[i]=j+1;
++i;
}while(i<yy);
Sort(main_Lotto,yy);
for(i=0;i<xx;i++)
{
j=0;
do
{
do
{
k=rand()%zz;
}while(double_Temp[i][k]!=1);
double_Temp[i][k]=0;
date_Lotto[i][j]=k+1;
++j;
}while(j<yy);
}
for(i=0;i<xx;i++)
Sort(date_Lotto[i],yy);
for(i=0;i<xx;i++)
{
if(is_Zero(date_Lotto[i],main_Lotto,yy))
++x0;
if(is_One(date_Lotto[i],main_Lotto,yy))
++x1;
if(is_Two(date_Lotto[i],main_Lotto,yy))
++x2;
if(is_Three(date_Lotto[i],main_Lotto,yy))
++x3;
if(is_Four(date_Lotto[i],main_Lotto,yy))
++x4;
if(is_Five(date_Lotto[i],main_Lotto,yy))
++x5;
if(is_Six(date_Lotto[i],main_Lotto,yy))
++x6;
}
printf("Random coupon: (6/49) : \n\n");
for(i=0;i<yy;i++)
{
printf("%d ",main_Lotto[i]);
}
printf("\n\n\n");
printf("After simulating a thausan draws. Results : \n\n");
printf("0: %d\n",x0);
printf("1: %d\n",x1);
printf("2: %d\n",x2);
printf("3: %d\n",x3);
printf("4: %d\n",x4);
printf("5: %d\n",x5);
printf("6: %d\n",x6);
free(main_Lotto);
free(Temp);
for(i=0;i<xx;i++)
{
free(date_Lotto[i]);
free(double_Temp[i]);
}
free(date_Lotto);
free(double_Temp);
return 0;
}
czwartek, 4 lipca 2024
Draw simulation based on the example of a lottery - C (gcc)
Subskrybuj:
Posty (Atom)