lottery

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2015 License: MIT Imports: 4 Imported by: 0

README

lottery

Circle CI Coverage Status GoDoc

lottery is rand base wrapper library

Install

$ go get github.com/kyokomi/lottery

Usage

import "github.com/kyokomi/lottery"

Example

Simple lottery
package main

import (
	"fmt"
	"math/rand"
	"time"

	"github.com/kyokomi/lottery"
)

func main() {
	l := lottery.New(rand.New(rand.NewSource(time.Now().UnixNano())))

	check := 1000000
	prob := 4
	count := 0
	for i := 0; i < check; i++ {
		if l.Lot(prob) {
			count++
		}
	}
	fmt.Println(float32(count) / float32(check) * 100, "%")
}
Multiple lottery
package main

import (
	"fmt"
	"math/rand"
	"time"
	"log"

	"github.com/kyokomi/lottery"
)

type DropItem struct {
	ItemName string
	DropProb int
}

func (d DropItem) Prob() int {
	return d.DropProb
}

var _ lottery.Interface = (*DropItem)(nil)

type Trap struct {
	TrapName string
	prob     int
}

func (t Trap) Prob() int {
	return t.prob
}

var _ lottery.Interface = (*Trap)(nil)

func main() {
	l := lottery.New(rand.New(rand.NewSource(time.Now().UnixNano())))

	dropItems := []lottery.Interface{
		DropItem{ItemName: "エリクサ", DropProb: 5},
		DropItem{ItemName: "エーテル", DropProb: 10},
		DropItem{ItemName: "ポーション", DropProb: 20},
		DropItem{ItemName: "ハズレ", DropProb: 50},
		Trap{TrapName: "地雷", prob: 5},
		Trap{TrapName: "トラバサミ", prob: 10},
	}

	check := 2000000
	countMap := map[lottery.Interface]int{}
	for i := 0; i < check; i++ {
		lotIdx := l.Lots(dropItems...)
		if lotIdx == -1 {
			log.Fatalln("lot error")
		}

		switch d := dropItems[lotIdx].(type) {
		case DropItem, Trap:
			countMap[d]++
		}
	}

	for item, count := range countMap {
		result := float64(count) / float64(check) * 100
		prob := float64(item.Prob())

		name := ""
		switch t := item.(type) {
		case DropItem:
			name = t.ItemName
		case Trap:
			name = t.TrapName
		}

		// 0.1 check
		if (prob-0.1) <= result && result < (prob+0.1) {
			fmt.Printf("ok %3.5f%%(%7d) : %s\n", result, count, name)
		} else {
			log.Fatalln("error %3.5f%%(%7d) : %s\n", result, count, name)
		}
	}
}

Auther

kyokomi

License

MIT

Documentation

Overview

Package lottery math/rand based lottery library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Interface

type Interface interface {
	Prob() int
}

Interface provide an interface to handle multiple lottery object.

type Lottery

type Lottery interface {
	Lot(prob int) bool
	LotOf(prob int, totalProb int) bool
	Lots(lots ...Interface) int
}

Lottery math/rand wrapper.

func New

func New(rd *rand.Rand) Lottery

New return lottery library.

func NewDefault

func NewDefault() Lottery

NewDefault return default rand lottery library.

type MockInterface

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

Mock of Interface interface

func NewMockInterface

func NewMockInterface(ctrl *gomock.Controller) *MockInterface

func (*MockInterface) EXPECT

func (_m *MockInterface) EXPECT() *_MockInterfaceRecorder

func (*MockInterface) Prob

func (_m *MockInterface) Prob() int

type MockLottery

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

Mock of Lottery interface

func NewMockLottery

func NewMockLottery(ctrl *gomock.Controller) *MockLottery

func (*MockLottery) EXPECT

func (_m *MockLottery) EXPECT() *_MockLotteryRecorder

func (*MockLottery) Lot

func (_m *MockLottery) Lot(prob int) bool

func (*MockLottery) LotOf

func (_m *MockLottery) LotOf(prob int, totalProb int) bool

func (*MockLottery) Lots

func (_m *MockLottery) Lots(lots ...Interface) int

Jump to

Keyboard shortcuts

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