// lotto2.c rolf.freitag at email.de 2000 // For producing random Lotto numbers (1..49) which are really equally distributed. // Todo: Buffering +qsort of each row of numbers. #include #include #include #include #include // simple version number #define SOFTWARE_VERSION_NUMBER 1.0 int main (const int argc, const char * const argv[]) { int i = 0; unsigned char i1 = 0, i2 = 0; unsigned long int lc = 0; unsigned char c7[7] = { 0 }; FILE *ifp = NULL, *ofp = NULL; char inname[0x100] = "/dev/urandom"; // input file name char outname[0x100] = "./lotto2.out"; // output file name if ( (argc>1) and not strncmp (argv[1], "-V", 123) ) { fprintf (stdout, "%s version %.1f\n", argv[0], SOFTWARE_VERSION_NUMBER); exit ( (2 == argc) ? 0 : -1); } ifp = fopen (inname, "rb"); if (ifp == NULL) { fprintf (stderr, "\a\ncan't open %s ", inname); perror("\n error \n"); exit (errno); // missing output file } ofp = fopen (outname, "w"); if (ofp == NULL) { fprintf (stderr, "\a\ncan't open %s ", outname); perror("\n error \n"); exit (errno); // missing output file } fprintf (ofp, "True random Lotto numbers (1..49).\n"); for (i = 0; i < 59 * 3 * 7; i++) /* 59 lines with 3 times 6 numbers */ { i2 = 0; while (i2 == 0) // while no usable random number { fscanf (ifp, "%c", &i1); // read a true random byte if (i1 <= 244) { i2 = i1 / 5 + 1; /* 0 <= i1 <= 244 implies 1 <= i2 <= 49 */ /* this loop is done approx. 1,045 times */ for (i1 = 0; i1 < lc % 7; i1++) /* avoid two times the same number in a row of 7 numbers */ if (c7[i1] == i2) /* same number */ { i2 = 0; break; /* not the same number */ } else c7[lc % 7] = i2; /* Store the actual and valid value to avoid two times the same number. */ } } /* while, a true random Lotto number has been prduced */ if (lc % 7 != 0) // 6 numbers if (i2 > 9) fprintf (ofp, "%u ", i2); else fprintf (ofp, " %u ", i2); else fprintf (ofp, " "); fflush (ofp); lc++; if (lc % 7 == 0) { fprintf (ofp, "\t"); for (i1 = 0; i1 < 7; i1++) c7[i1] = 0; if (lc % 3 == 0) /* x % 7 = 0 u. x % 3 = 0 implies x % 21 = 0 */ fprintf (ofp, "\n"); /* line end */ } } /* for */ fclose (ifp); return (0); }