package lotteryimport ("fmt""math/rand""sort""time" )type Lottery struct { }func NewLottery() *Lottery {return &Lottery{} }type Prize struct {Name stringStock intWeight int //权重 }// SortPrizes 按权重生序 func (l *Lottery) SortPrizes(prizes []Prize) {sort.Slice(prizes, func(i, j int) bool {return prizes[i].Weight < prizes[j].Weight}) }// CreateWeightRanges 创建权重区间 func (l *Lottery) CreateWeightRanges(prizeList []Prize) []int {var weightRanges []inttotalWeight := 0for _, item := range prizeList {totalWeight = item.Weight + totalWeightweightRanges = append(weightRanges, totalWeight)}return weightRanges }func (l *Lottery) DrawPrize(prizeList []Prize, weightRanges []int) string {//生成一个随机数//播种rand.Seed(time.Now().UnixNano())randNum := rand.Intn(weightRanges[len(weightRanges)-1])fmt.Println("随机数=", randNum)fmt.Println("权重slice=", weightRanges)for i, rangeNum := range weightRanges {if randNum < rangeNum {return prizeList[i].Name}}return "" }func TestDraw() {// 定义奖品信息及库存prizes := []Prize{{"一等奖 iPhone 16 Max", 1, 1},{"二等奖 iPad", 1, 1},{"三等奖 苹果耳机", 1, 1},{"四等奖 红米手机", 5, 4},{"五等奖 苹果手机充电器", 10, 5},{"六等奖 红牛", 20, 8},{"七等奖 矿泉水", 20, 10},{"八等奖 微信10元立减金", 30, 12},{"九等奖 微信3元立减金", 30, 14},}p := NewLottery()p.SortPrizes(prizes)weightRanges := p.CreateWeightRanges(prizes)for i := 0; i < 1; i++ {result := p.DrawPrize(prizes, weightRanges)fmt.Printf("抽中奖品:%s\n", result)} }
权重值约大,中奖概率越大。逻辑是控制随机数落到每个奖品的范围概率。