Skip to content
C++ Better Explained
Go back

How to Create a Lottery Program in C++ - Step by Step with Source Code

Edit page

This C++ practice assignment requires us to write a lottery program that randomly generates the numbers of a lottery ticket and outputs the values. It is a great exercise for practising arrays, for loops, and random number generation in C++.

Requirements

The specific requirements for this program are:

Concepts Covered

Full Source Code

// Lottery ticket generator
// Author: Sahil Bora
// File: main.cpp

#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

// Constants
const int MIN  = 1;
const int MAX  = 45;
const int COLS = 6;
const int ROWS = 12;

// Function prototypes
void getLine(int line[]);
void printLine(int line[]);
bool duplicates(int num, int arr[], int size);

int main() {
    int row[COLS] = {0};
    srand(time(NULL));

    cout << "Your Tattslotto ticket\n\n";

    for (int i = 0; i < ROWS; i++) {
        getLine(row);
        printLine(row);
    }

    return 0;
}

// Check if a number already exists in the array
bool duplicates(int num, int arr[], int size) {
    for (int j = 0; j < size; j++) {
        if (arr[j] == num)
            return true;
    }
    return false;
}

// Generate a single lottery line with no duplicate numbers
void getLine(int line[]) {
    int rNum;
    for (int i = 0; i < COLS; i++) {
        rNum = rand() % (MAX - MIN + 1) + MIN;
        while (duplicates(rNum, line, i) == true)
            rNum = rand() % (MAX - MIN + 1) + MIN;
        line[i] = rNum;
    }
}

// Print a single lottery line
void printLine(int line[]) {
    for (int i = 0; i < COLS; i++)
        cout << "\n" << line[i];
    cout << endl << endl;
}

Step by Step Breakdown

Constants

const int MIN  = 1;
const int MAX  = 45;
const int COLS = 6;
const int ROWS = 12;

Using constants makes the program easy to modify. If you want to change the range or the number of rows, you only need to update these four values rather than hunting through the code.

Seeding the Random Number Generator

srand(time(NULL));

srand() seeds the random number generator with the current time. Without this, rand() would produce the same sequence of numbers every time the program runs. Seeding with time(NULL) ensures a different sequence on each run.

Generating Random Numbers Without Duplicates

void getLine(int line[]) {
    int rNum;
    for (int i = 0; i < COLS; i++) {
        rNum = rand() % (MAX - MIN + 1) + MIN;
        while (duplicates(rNum, line, i) == true)
            rNum = rand() % (MAX - MIN + 1) + MIN;
        line[i] = rNum;
    }
}

The formula rand() % (MAX - MIN + 1) + MIN generates a random number between 1 and 45. After generating a number, it checks whether that number already exists in the current row using the duplicates() function. If it does, it generates a new number and checks again — repeating until a unique number is found.

Duplicate Detection

bool duplicates(int num, int arr[], int size) {
    for (int j = 0; j < size; j++) {
        if (arr[j] == num)
            return true;
    }
    return false;
}

This function loops through the array up to the current position and returns true if the number already exists, or false if it is unique. Only the filled portion of the array is checked — the size parameter controls how far to look.

The Main Loop

for (int i = 0; i < ROWS; i++) {
    getLine(row);
    printLine(row);
}

This loop runs 12 times — once for each lottery ticket row. Each iteration generates a fresh set of 6 unique numbers and prints them.

Example Output

Your Tattslotto ticket

3
17
42
8
31
25

12
6
39
44
2
19

...

Summary

This C++ lottery program covers some essential beginner concepts — arrays, for loops, random number generation, and function design. The duplicate detection logic is a good introduction to writing utility functions that keep your main code clean and readable.

Key takeaways:


Want more C++ projects like this? C++ Better Explained walks you through real-world C++ from beginner to advanced — grab the book and start building today.


Edit page
Share this post on:

Next Post
C++ Traffic Light Project - Detailed Explanation with Source Code