Thread: Lottery program, no duplicates (without user input!)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question Lottery program, no duplicates (without user input!)

    I know that there are many posts of this on the internet but I only see the lottery programs using input from a user. That would make it a lot easier to check if the same number where entered again.

    But in my case I have to generate a random lottery row of 7 numbers, interval 1 - 35. And I have to remove the duplicates if there are any.

    My first thought was to use a nested for-loop, but then I realized that my algorithm didn't work. That's because the elements in the array haven't been initialized in the outer for-loop. If I try to compare row[0] == row[i] in the inner for-loop it means I compare to unitialized elements in the array.

    Also what's wrong is that when I do the if-statement I cannot be sure that the random number is different, it could be the same number again.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    #define MAX_VALUE 5
    #define SIZE 7
    
    
    void printArray(int arr[], int size); 
    
    
    int main() {
    	
    	srand(time(NULL)); 
    	
    	int row[SIZE] = {0}; 
    	int i; 
    	int counter = 0;
    	for (i = 0; i < SIZE; i++) {
    		int randNum = rand()%MAX_VALUE + 1;
    		row[i] = randNum;
    		
    		int j; 
    		for (j = 0; j < SIZE; j++) {
    			if (row[counter] == row[i]) {
    				row[i] = rand()%MAX_VALUE + 1;
    			}
    		}
    		counter++;
    	}
    	
    	printArray(row, SIZE);
    	
    	
    	return 0; 
    }
    
    
    void printArray(int arr[], int size) {
    	int i; 
    	for (i = 0; i < size; i++)
    		printf("%d, ", arr[i]); 
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    The easy way
    1. Fill an array with the numbers 1 to 35 inclusive (hint, it has 35 elements)
    2. In a loop, randomly swap pairs of elements (it's called shuffling - think deck of cards).
    3. Then draw as many numbers as you like from the array - they will always be unique, but in a random order.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-21-2015, 07:17 AM
  2. C program does not take input from user
    By Izzy98 in forum C Programming
    Replies: 4
    Last Post: 11-06-2015, 03:26 PM
  3. Array's if/else Can't reject user input duplicates
    By andrew.comly in forum C Programming
    Replies: 5
    Last Post: 03-25-2015, 07:35 AM
  4. Want User Input but Program Ends
    By completenewbieu in forum C Programming
    Replies: 5
    Last Post: 08-29-2011, 04:10 PM
  5. Replies: 3
    Last Post: 10-12-2010, 01:40 PM

Tags for this Thread