每次买彩票在彩票站机选的号码基本都中不了,于是想着自己也手写一个随机生成彩票号码的程序。下面是生成双色球和大乐透的实现代码。
双色球
红球需要从 1- 33 的号码中,选出6个号码; 篮球需要从1-16的号码中,选出1个号码。即 6红 1蓝
大乐透
红球需要从 1- 35 的号码中,选出5个号码; 篮球需要从1-12的号码中,选出2个号码。即 5红 2蓝
package lottery
import (
"fmt"
"math/rand"
"sort"
"testing"
"time"
)
var (
doubleRed = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33}
doubleBlue = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
bigRed = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35}
bigBlue = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
)
// TestDoubleBall 双色球
func TestDoubleBall(t *testing.T) {
num := 5
balls := randDoubleBall(num)
fmt.Println("[双色球]", num, "注")
nicePrint(balls)
}
// TestBigHappy 大乐透
func TestBigHappy(t *testing.T) {
num := 5
balls := randBigHappy(num)
fmt.Println("[大乐透]", num, "注")
nicePrint(balls)
}
// Balls 选球
type Balls struct {
RedBalls []int
BlueBall []int
}
// randDoubleBall 随机生成 多组双色球号码
func randDoubleBall(num int) []*Balls {
var result []*Balls
rand.Seed(time.Now().UnixNano())
for i := 0; i < num; i {
doubleBall := &Balls{}
doubleRedCopy := make([]int, len(doubleRed))
copy(doubleRedCopy, doubleRed)
for j := 0; j < 6; j {
redBallIndex := rand.Intn(len(doubleRedCopy) - 1)
doubleBall.RedBalls = append(doubleBall.RedBalls, doubleRedCopy[redBallIndex])
doubleRedCopy = removeItem(doubleRedCopy, redBallIndex)
time.Sleep(time.Nanosecond * time.Duration(redBallIndex))
}
doubleBall.RedBalls = sortItems(doubleBall.RedBalls)
blueBallIndex := rand.Intn(len(doubleBlue) - 1)
doubleBall.BlueBall = append(doubleBall.BlueBall, doubleBlue[blueBallIndex])
result = append(result, doubleBall)
time.Sleep(time.Nanosecond * time.Duration(blueBallIndex))
}
return result
}
// randBigHappy 随机生成多组大乐透号码
func randBigHappy(num int) []*Balls {
var result []*Balls
rand.Seed(time.Now().UnixNano())
for i := 0; i < num; i {
bigBall := &Balls{}
bigRedCopy := make([]int, len(doubleRed))
bigBlueCopy := make([]int, len(bigBlue))
copy(bigRedCopy, bigRed)
copy(bigBlueCopy, bigBlue)
for j := 0; j < 5; j {
redBallIndex := rand.Intn(len(bigRedCopy) - 1)
bigBall.RedBalls = append(bigBall.RedBalls, bigRedCopy[redBallIndex])
bigRedCopy = removeItem(bigRedCopy, redBallIndex)
time.Sleep(time.Nanosecond * time.Duration(redBallIndex))
}
bigBall.RedBalls = sortItems(bigBall.RedBalls)
for k := 0; k < 2; k {
blueBallIndex := rand.Intn(len(bigBlueCopy) - 1)
bigBall.BlueBall = append(bigBall.BlueBall, bigBlueCopy[blueBallIndex])
bigBlueCopy = removeItem(bigBlueCopy, blueBallIndex)
time.Sleep(time.Nanosecond * time.Duration(blueBallIndex))
}
bigBall.BlueBall = sortItems(bigBall.BlueBall)
result = append(result, bigBall)
}
return result
}
// nicePrint 美化输出打印
func nicePrint(balls []*Balls) {
for i := 0; i < len(balls); i {
fmt.Print("红球:")
for j := 0; j < len(balls[i].RedBalls); j {
fmt.Print(" ", balls[i].RedBalls[j])
}
fmt.Println()
fmt.Print("蓝球:")
for j := 0; j < len(balls[i].BlueBall); j {
fmt.Print(" ", balls[i].BlueBall[j])
}
fmt.Println()
fmt.Println()
}
}
// sortItems 号码排序
func sortItems(s []int) []int {
sort.Ints(s)
return s
}
// removeItem 移除元素
func removeItem(s []int, index int) []int {
s = append(s[:index], s[index 1:]...)
return s
}
测试结果:
代码语言:go复制[双色球] 5 注
红球: 10 12 26 29 31 32
蓝球: 13
红球: 4 12 15 18 19 24
蓝球: 2
红球: 6 13 14 16 20 32
蓝球: 13
红球: 3 6 12 25 31 32
蓝球: 3
红球: 7 13 14 17 29 30
蓝球: 9
[大乐透] 5 注
红球: 7 8 14 16 24
蓝球: 11 14
红球: 5 8 12 13 25
蓝球: 5 13
红球: 6 13 19 27 32
蓝球: 4 14
红球: 15 17 18 31 32
蓝球: 12 13
红球: 12 17 19 30 31
蓝球: 1 3