-
Random Number Help
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);
}
-
Code:
for (i = 0; i <= 5; i++){
fnum = 0;
if ( pick != NoSame[i] ){
fnum = pick;
i = 5;
} else {
pick = WPicks();
i -= 1;
}
}
What do you think this code does?
Hint: Why do you think none of the repeated numbers appear in the first position?
-
It compares pick to see if it is not equal to each index of the array. If it is equal to the array index it goes to the else statement and gets another number for pick and minuses off 1 from i to run the loop again.
Or am I wrong and that NoSame[i] is only a pointer to the first index of the array?
-
Okay, now we know what you want to do. Now you need to exercise your reading skills and see what it actually does do. Suppose the array NoSame is [36, 17, 0, 0, 0, 0] and pick is 17. You know what you want your loop to do. What does your loop actually do?
-
I think i figured it out. I had 2 mistakes in the if statement if pick was not eqaul to NoSame[i] it assigned pick to fnum and drops out of the loop with out comparing it to the other numbers. My second mistake was in the else statement i -=1; should be i = 0; to go back and check it against all the numbers again.
Here is my new code:
Code:
for (i = 0; i <= 5; i++){
fnum = 0;
if ( pick != NoSame[i] ){
fnum = pick;
//i = 5;
} else {
pick = WPicks();
i = 0;
}
}
Thanks tabsstop