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

55,449 answers

573 users

How to generate random lottery numbers for 6 numbers out of 37 and 1 power number out of 7 in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "math/rand"
    "sort"
    "time"
)

/*
    This program generates random lottery numbers for:
        - 6 distinct numbers out of 37
        - 1 distinct number out of 7 (power number)

    It uses:
        - math/rand with time-based seeding
        - rand.Shuffle() for efficient unique selection
        - clean functions and idiomatic Go style
*/

/*
    pickDistinctNumbers(count, max):
    Generates 'count' distinct random numbers from the range [1..max].

    Algorithm:
        - Create a slice containing all numbers 1..max
        - Shuffle the slice using rand.Shuffle()
        - Take the first 'count' numbers

    This guarantees:
        - all numbers are unique
        - uniform randomness
        - no duplicate checks needed
*/
func pickDistinctNumbers(count, max int) []int {
    numbers := make([]int, max)
    for i := 0; i < max; i++ {
        numbers[i] = i + 1
    }

    rand.Shuffle(len(numbers), func(i, j int) {
        numbers[i], numbers[j] = numbers[j], numbers[i]
    })

    return numbers[:count]
}

/*
    pickPowerNumber(max):
    Returns a single random number in the range [1..max].
*/
func pickPowerNumber(max int) int {
    return rand.Intn(max) + 1
}

func main() {
    rand.Seed(time.Now().UnixNano()) // Seed RNG

    mainCount := 6
    mainMax := 37
    powerMax := 7

    // Generate main numbers (distinct)
    mainNumbers := pickDistinctNumbers(mainCount, mainMax)
    sort.Ints(mainNumbers) // sort for nicer output

    // Generate power number
    powerNumber := pickPowerNumber(powerMax)

    // Output results
    fmt.Printf("Main numbers (6 out of 37): %v\n", mainNumbers)
    fmt.Printf("Power number (1 out of 7): %d\n", powerNumber)
}


/*
run:

Main numbers (6 out of 37): [10 13 19 22 26 34]
Power number (1 out of 7): 4

*/

 



answered Jul 28 by avibootz

Related questions

...