I'm very new to C programming, so I am trying to write a simple random number generator to pick lotto numbers. But sometimes the program list the same number multiple times per ticket. I added an if statement to check if the pick is the same as any previous number but sometimes it prints multiple numbers on the same ticket. I am having problems in the DifferentNumbers ( ) function. Here is a sample of the print out:
55 19 26 47 29 09
20 51 51 55 51 54 <-- 51 printed 3 times
46 09 02 23 28 30
56 50 31 04 09 10
54 38 25 30 26 08
37 02 50 33 13 05
21 38 11 26 55 56
18 23 01 44 13 56
01 32 32 28 51 13 <-- 32 printed 2 times
16 50 49 22 52 10
Code:
#include <stdio.h>
#include <time.h> //This is to bring in the declaration of clock()
#include <stdlib.h> //This is to bring in the declarations of srand() and rand()
int WPicks( void ); // Random Numbers for Weekly picks 1 - 56
int DPicks( void ); // Random Numbers for Daily picks 0 - 9
void Weekly( void ); // Weekly Picks Function
void Daily( void ); // Daily Picks Function
int DifferentNumbers ( void ); // Makes sure that all numbers are differnet
int NoSame[8]; // Array to check if numbers are the same
int main (int argc, const char * argv[]) {
srand( clock() );
Weekly();
//printf("\n"); //Commented out these 2 function to figure of Weely
//Daily(); //Functions Same Number Problem
return 0;
}
/* Weekly Picks Function */
void Weekly(){
int i, j, k;
for (k=0; k<=5; k++) {
NoSame[k] = 0;
}
for (j = 1; j <= 20; j++){ //Number of Picks on Ticket ie 20 tickets
for (i = 0; i <= 5; i++) {
NoSame[i] = DifferentNumbers();
}
for (i=0; i<=5; i++) {
printf( "%2d\t",NoSame[i]);
}
printf("\n");
for (k=0; k<=5; k++) {
NoSame[k] = 0;
}
}
}
/* Makes sure that all numbers are differnet */
int DifferentNumbers ( ){
int pick, i, fnum;
pick = WPicks();
for (i = 0; i <= 5; i++){
fnum = 0;
if ( pick != NoSame[i] ){
fnum = pick;
i = 5;
} else {
pick = WPicks();
i -= 1;
}
}
return fnum;
}
/* Daily Picks Function */
void Daily(){
int pick, i, j;
for (j=1; j<=5; j++){
for (i=1; i<=3; i++) {
pick = DPicks();
printf( "%d\t",pick);
}
printf("\n");
}
}
/* Random Numbers for Weekly picks 1 - 56 */
int WPicks( void ) {
return (rand() % 56) + 1;
}
/* Random Numbers for Daily picks 0 - 9 */
int DPicks( void ) {
return (rand() % 10);
}