Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,696 questions

55,455 answers

573 users

How to generate random Powerball lottery numbers (pick 5 numbers from 1-69 + 1 Powerball from 1-26) in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

int cmp_int(const void *a, const void *b) {
    int ia = *(const int *)a;
    int ib = *(const int *)b;
    
    return (ia > ib) - (ia < ib);   // returns +1, 0, -1
}

int main(void)
{
    int r, a, b, line[5];
    bool dup;

    srand((unsigned)time(NULL));

    printf("                          POWER\n");

    for (int n = 0; n < 4; n++)
    {
        a = 1;
        b = 69;

        for (int i = 0; i < 5; i++) {
            do {
                r = rand() % (b - a + 1) + a; // 1–69
                dup = false;

                for (int j = 0; j < i; j++) {
                    if (line[j] == r) {
                        dup = true;
                        break;
                    }
                }
            } while (dup);

            line[i] = r;
        }

        // Sort the line using qsort
        qsort(line, 5, sizeof(int), cmp_int);

        // Print main numbers
        for (int i = 0; i < 5; i++)
            printf("%3d ", line[i]);

        printf(" QP - ");

        // Powerball
        r = rand() % 26 + 1;
        printf("%3d  QP\n", r);
    }

    return 0;
}



/*
run:
 
                          POWER
  5  34  45  60  69  QP -   9  QP
  5  18  21  29  34  QP -  18  QP
 22  28  29  37  50  QP -  23  QP
 17  23  29  50  59  QP -  16  QP
  
*/
 

 



answered Jan 30, 2016 by avibootz
edited Jul 28 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*
    This program generates random Powerball lottery numbers:
        - 5 distinct numbers from 1..69
        - 1 Powerball number from 1..26

    It uses:
        - srand(time(NULL)) to seed the RNG
        - rand() for random number generation
        - Fisher–Yates shuffle for efficient, uniform shuffling
*/

/*
    shuffle(array, n):
    Implements the Fisher–Yates shuffle.

    This algorithm:
        - runs in O(n)
        - produces a uniform random permutation
        - is the standard way to shuffle arrays correctly
*/
void shuffle(int *array, int n) {
    for (int i = n - 1; i > 0; i--) {
        int j = rand() % (i + 1);   // random index 0..i
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}

/*
    pickDistinctNumbers(count, max, result):
    Fills `result` with `count` distinct numbers from 1..max.

    Algorithm:
        - Build an array [1, 2, ..., max]
        - Shuffle it using Fisher–Yates
        - Copy the first `count` numbers into result[]
*/
void pickDistinctNumbers(int count, int max, int *result) {
    int *numbers = malloc(max * sizeof(int));
    if (!numbers) {
        fprintf(stderr, "Memory allocation failed\n");
        exit(1);
    }

    for (int i = 0; i < max; i++) {
        numbers[i] = i + 1;
    }

    shuffle(numbers, max);

    for (int i = 0; i < count; i++) {
        result[i] = numbers[i];
    }

    free(numbers);
}

/*
    pickPowerball(max):
    Returns a single random number in the range 1..max.
*/
int pickPowerball(int max) {
    return (rand() % max) + 1;
}

int main(void) {
    srand((unsigned)time(NULL));  // seed RNG

    const int MAIN_COUNT = 5;
    const int MAIN_MAX   = 69;
    const int POWER_MAX  = 26;

    int mainNumbers[MAIN_COUNT];

    // Generate main numbers (distinct)
    pickDistinctNumbers(MAIN_COUNT, MAIN_MAX, mainNumbers);

    // Sort for nicer output
    for (int i = 0; i < MAIN_COUNT - 1; i++) {
        for (int j = i + 1; j < MAIN_COUNT; j++) {
            if (mainNumbers[j] < mainNumbers[i]) {
                int temp = mainNumbers[i];
                mainNumbers[i] = mainNumbers[j];
                mainNumbers[j] = temp;
            }
        }
    }

    // Generate Powerball number
    int powerball = pickPowerball(POWER_MAX);

    // Output results
    printf("Powerball numbers (5 out of 69): ");
    for (int i = 0; i < MAIN_COUNT; i++) {
        printf("%d ", mainNumbers[i]);
    }
    printf("\nPowerball (1 out of 26): %d\n", powerball);

    return 0;
}


/*
run:

Powerball numbers (5 out of 69): 4 9 31 44 64 
Powerball (1 out of 26): 8

*/

 



answered Jul 28 by avibootz

Related questions

...