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

55,671 answers

573 users

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

1 Answer

0 votes
using System;
using System.Collections.Generic;

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

    This program uses:
        - System.Random for RNG
        - SortedSet<int> for uniqueness + sorted output
        - clean, idiomatic C# structure with helper functions
*/

class Program
{
    /*
        GenerateMainNumbers():
        Generates 5 UNIQUE numbers in the range [1, 69].
        SortedSet automatically:
            - prevents duplicates
            - keeps numbers sorted
    */
    static SortedSet<int> GenerateMainNumbers(Random rng)
    {
        var numbers = new SortedSet<int>();

        while (numbers.Count < 5) {
            int n = rng.Next(1, 70);   // 1–69
            numbers.Add(n);
        }

        return numbers;
    }

    /*
        GeneratePowerball():
        Generates a single number in the range [1, 26].
        Drawn from its own pool, independent of the main numbers.
    */
    static int GeneratePowerball(Random rng)
    {
        return rng.Next(1, 27);        // 1–26
    }

    static void Main()
    {
        Random rng = new Random();

        var mainNumbers = GenerateMainNumbers(rng);
        int powerball = GeneratePowerball(rng);

        Console.WriteLine("Random Powerball numbers:");
        Console.Write("Main numbers: ");

        foreach (int n in mainNumbers)
            Console.Write(n + " ");

        Console.WriteLine();
        Console.WriteLine("Powerball: " + powerball);
    }
}


/*
run:

Random Powerball numbers:
Main numbers: 15 41 51 53 59 
Powerball: 24

*/

 



answered Jul 29 by avibootz

Related questions

...