lottery

package module
v0.0.0-...-cc3a35a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

lottery

可以根据传递的概率快速抽奖, 并且解决了概率为浮点数时的精度问题

安装

使用go get安装本模块

go get -u github.com/shuaibingn/lottery

使用

  1. 首先需要使用匿名结构体*lottery.DrawBase来实现抽奖接口
  2. 构建一个抽奖切片, 里面包含ID, 概率等
  3. 使用InitLotteries函数初始化抽奖, 并且传递一个放大倍数, 保证概率和该数字的积为整数
  4. 调用Draw方法抽奖
package main

import (
	"fmt"

	"github.com/shuaibingn/lottery"
)

type Card struct {
	*lottery.DrawBase `json:",inline"`
	Name              string `json:"name"`
}

func main() {
	cards := []lottery.Lottery{
		&Card{&lottery.DrawBase{ID: "1", Probability: 0.1}, "SSR"},
		&Card{&lottery.DrawBase{ID: "2", Probability: 0.2}, "SR"},
		&Card{&lottery.DrawBase{ID: "3", Probability: 0.3}, "R"},
		&Card{&lottery.DrawBase{ID: "4", Probability: 0.4}, "N"},
	}

	lotteries, err := lottery.InitLotteries(cards, 10)
	if err != nil {
		panic(err)
	}

	id := lotteries.Draw()
	fmt.Println(id)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DrawBase

type DrawBase struct {
	ID          string  `json:"id"`
	Probability float64 `json:"probability"`
	// contains filtered or unexported fields
}

type Lotteries

type Lotteries struct {
	// contains filtered or unexported fields
}

func InitLotteries

func InitLotteries(data []Lottery, mul float64) (*Lotteries, error)

func (Lotteries) Draw

func (lotteries Lotteries) Draw() string

type Lottery

type Lottery interface {
	// contains filtered or unexported methods
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL