Created
February 10, 2021 16:57
-
-
Save DavidForster/fa8edbbb41ccab80de6a16d0eab7c331 to your computer and use it in GitHub Desktop.
go program to select Euromillions numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"sort" | |
"time" | |
) | |
type euroMillions struct { | |
Numbers [5]int | |
Luckystars [2]int | |
} | |
func (e *euroMillions) printDetails() { | |
sort.Ints(e.Numbers[:]) | |
sort.Ints(e.Luckystars[:]) | |
fmt.Print("Numbers: ", e.Numbers) | |
fmt.Println(", Lucky Stars: ", e.Luckystars) | |
} | |
func (e *euroMillions) luckyDip() { | |
var possibleNumbers = make([]int, 50) | |
for i := 0; i < len(possibleNumbers); i++ { | |
possibleNumbers[i] = i + 1 | |
} | |
for i := 0; i < len(e.Numbers); i++ { | |
e.Numbers[i], possibleNumbers = chooseLotteryNumber(possibleNumbers) | |
} | |
possibleLuckystars := make([]int, 12) | |
for i := 0; i < len(possibleLuckystars); i++ { | |
possibleLuckystars[i] = i + 1 | |
} | |
for i := 0; i < len(e.Luckystars); i++ { | |
e.Luckystars[i], possibleLuckystars = chooseLotteryNumber(possibleLuckystars) | |
} | |
} | |
func main() { | |
rand.Seed(time.Now().UTC().UnixNano()) | |
lottery := euroMillions{} | |
lottery.luckyDip() | |
lottery.printDetails() | |
} | |
func chooseLotteryNumber(numbers []int) (int, []int) { | |
index := rand.Intn(len(numbers) - 1) | |
chosenNumber := numbers[index] | |
return chosenNumber, removeIndex(numbers, index) | |
} | |
func removeIndex(slice []int, index int) []int { | |
return append(slice[:index], slice[index+1:]...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment