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,943 questions

55,787 answers

573 users

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

1 Answer

0 votes
import Foundation

/*
    Generate random Powerball lottery numbers:
        - 5 distinct numbers from 1–69
        - 1 distinct Powerball number from 1–26

    This program uses:
        - Swift's Int.random(in:) for RNG
        - Set<Int> for uniqueness
        - clean, idiomatic functions
*/

/*
    generateMainNumbers():
    Generates 5 UNIQUE numbers in the range 1...69.
    A Set ensures no duplicates.
*/
func generateMainNumbers() -> [Int] {
    var chosen: Set<Int> = []

    // Keep adding until we have 5 distinct numbers
    while chosen.count < 5 {
        let n: Int = Int.random(in: 1...69)
        chosen.insert(n)
    }

    // Convert to sorted array for nice output
    return Array(chosen).sorted()
}

/*
    generatePowerball():
    Generates a single number in the range 1...26.
*/
func generatePowerball() -> Int {
    Int.random(in: 1...26)
}

/*
    Main:
    Generate and print the Powerball ticket.
*/
let mainNumbers: [Int] = generateMainNumbers()
let powerball: Int = generatePowerball()

print("Random Powerball numbers:")
print("Main numbers: \(mainNumbers.map(String.init).joined(separator: " "))")
print("Powerball: \(powerball)")


/*
run:

Random Powerball numbers:
Main numbers: 11 29 33 52 58
Powerball: 24

*/

 



answered Jul 30 by avibootz

Related questions

...