Thread: Random Number Help

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Daytona Beach FL
    Posts
    3

    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);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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?

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    Daytona Beach FL
    Posts
    3
    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?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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?

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    Daytona Beach FL
    Posts
    3
    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
    Last edited by syclonefx; 06-11-2009 at 11:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. random number tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 02:41 PM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM