/**********************************************************************/ /* */ /* Program: lotto.c */ /* */ /* Description: */ /* ============ */ /* */ /* This program creates rows for the Swedish lottery "Lotto". */ /* Each row consists of 7 random number between 1 and 35 */ /* all different. */ /* */ /* The seed for the random numbers can be selected manually or */ /* initialized automatically. All input parameters are integers. */ /* */ /* Author: */ /* ======= */ /* */ /* Stefan Spaennare, Lund, Sweden */ /* */ /* E-mail: stefan@spaennare.se */ /* */ /* First version: February 1997 */ /* Latest update: 2007-07-28 */ /* */ /**********************************************************************/ /********************************************************************************/ /* */ /* Notice */ /* ====== */ /* */ /* I make no warranties that this program is (1) free of errors, (2) consistent */ /* with any standard merchantability, or (3) meeting the requirements of a */ /* particular application. This software shall not, partly or as a whole, */ /* participate in a process, whose outcome can result in injury to a person or */ /* loss of property. It is solely designed for analytical work. Permission to */ /* use, copy and distribute is hereby granted without fee, providing that the */ /* header above including this notice appears in all copies. */ /* */ /* Stefan Spaennare */ /* */ /********************************************************************************/ #include #include #include #include #include #include double timetic=(double)(CLOCKS_PER_SEC); #define IM1 2147483563 #define IM2 2147483399 #define AM (1.0/IM1) #define IMM1 (IM1-1) #define IA1 40014 #define IA2 40692 #define IQ1 53668 #define IQ2 52774 #define IR1 12211 #define IR2 3791 #define NTAB 32 #define NDIV (1+IMM1/NTAB) #define EPS 1.2e-7 #define RNMX (1.0-EPS) double ran2(idum) long *idum; { int j; long k; static long idum2=123456789; static long iy=0; static long iv[NTAB]; double temp; if (*idum <= 0) { if (-(*idum) < 1) { *idum=1; } else { *idum=-(*idum); } /* if */ idum2=(*idum); for (j=NTAB+7; j>=0; j--) { k=(*idum)/IQ1; *idum=IA1*(*idum-k*IQ1)-k*IR1; if (*idum < 0) { *idum += IM1; } /* if */ if (j < NTAB) { iv[j]=*idum; } /* if */ } /* for j */ iy=iv[0]; } /* if */ k=(*idum)/IQ1; *idum=IA1*(*idum-k*IQ1)-k*IR1; if (*idum < 0) { *idum += IM1; } /* if */ k=idum2/IQ2; idum2=IA2*(idum2-k*IQ2)-k*IR2; if (idum2 < 0) { idum2 += IM2; } /* if */ j=iy/NDIV; iy=iv[j]-idum2; iv[j] = *idum; if (iy < 1) { iy += IMM1; } /* if */ temp=AM*(double)(iy); if (temp > RNMX) { return(RNMX); } else { return(temp); } /* if */ } /* ran2 */ void qqsort(f,n) int *f; int n; { int l,r,s,m,i,j,temp; int sr[33]; int sl[33]; s=1; sl[s]=1; sr[s]=n; do { l=sl[s]; r=sr[s]; s--; do { i=l; j=r; m=f[(l+r)/2]; do { while (f[i] < m) { i++; } /* while */ while (f[j] > m) { j--; } /* while */ if (i <= j) { temp=f[i]; f[i]=f[j]; f[j]=temp; i++; j--; } /* if */ } while (i <= j); if (i < r) { s++; sl[s]=i; sr[s]=r; } /* if */ r=j; } while (l < r); } while (s > 0); } /* qqsort */ int main(argc,argv) int argc; char *argv[]; { int i,j,k,s,n,nrows,flag,seed; long idum; double ttt1,ttt2; time_t tt; int *v; if (argc != 3) { printf("Usage: %s seedi nrowsi\n",argv[0]); printf("\n"); printf("seed: Random seed > 0. If seed = 0 the random numbers are initialized automatically.\n"); printf("nrows: Number of Lotto rows.\n"); printf("\n"); exit(0); } /* if */ seed=atoi(argv[1]); nrows=atoi(argv[2]); if (seed == 0) { tt=time(0); ttt1=(double)(tt)+0.5; ttt2=fmod(ttt1,2147483647.0); seed=(long)(ttt2); idum=-seed-1; } else { idum=-abs(seed)-1; } /* if */ n=7; v = (int *)calloc(n+1,sizeof(int)); for (i=1; i<=nrows; i++) { j=1; while (j <= n) { s=(int)(35.0*ran2(&idum))+1; k=1; flag=0; while ((k < j) && (flag == 0)) { if (s == v[k]) { flag=1; } /* if */ k++; } /* while */ if (flag == 0) { v[j]=s; j++; } /* if */ } /* while */ qqsort(v,n); printf("%4d:",i); for (j=1; j<=n; j++) { printf("%3d",v[j]); } /* for j */ printf("\n"); } /* for i */ } /* End */