package tool

import (
	"math/rand"
	"time"
)

type Lottery struct {
	ID     uint64
	Weight int
}

// DoLottery 抽奖
// randSeed 额外的随机种子
func DoLottery(lotteries []Lottery, randSeed int64) uint64 {
	rand.Seed(time.Now().UnixNano() + randSeed)
	totalWeight := 0
	for _, p := range lotteries {
		totalWeight += p.Weight
	}
	winningNumber := rand.Intn(totalWeight)
	currentWeight := 0
	var winner Lottery
	for _, p := range lotteries {
		currentWeight += p.Weight
		if winningNumber < currentWeight {
			winner = p
			break
		}
	}
	return winner.ID
}
