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

55,437 answers

573 users

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

1 Answer

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

    This program uses:
        - crypto.getRandomValues() when available (browser)
        - Math.random() fallback for Node.js
        - Set for uniqueness + sorted output
*/

/*
    getRandomInt(min, max):
    Returns a random integer in the inclusive range [min, max].
    Uses crypto.getRandomValues() when available for better randomness.
*/
function getRandomInt(min, max) {
    if (typeof crypto !== "undefined" && crypto.getRandomValues) {
        const array = new Uint32Array(1);
        crypto.getRandomValues(array);
        return min + (array[0] % (max - min + 1));
    }
    // Node.js fallback
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

/*
    generateMainNumbers():
    Generates 5 UNIQUE numbers in the range [1, 69].
    Uses a Set to enforce uniqueness.
*/
function generateMainNumbers() {
    const numbers = new Set();

    while (numbers.size < 5) {
        numbers.add(getRandomInt(1, 69));
    }

    return [...numbers].sort((a, b) => a - b);
}

/*
    generatePowerball():
    Generates a single number in the range [1, 26].
*/
function generatePowerball() {
    return getRandomInt(1, 26);
}

/*
    Main:
    Generate and print the Powerball ticket.
*/
const mainNumbers = generateMainNumbers();
const powerball = generatePowerball();

console.log("Random Powerball numbers:");
console.log("Main numbers:", mainNumbers.join(" "));
console.log("Powerball:", powerball);


/*
run:

Random Powerball numbers:
Main numbers: 6 11 15 23 49
Powerball: 11

*/

 



answered Jul 30 by avibootz

Related questions

...