Last active
August 29, 2015 14:25
-
-
Save tenntenn/2d0977d317222339cd2e to your computer and use it in GitHub Desktop.
lottery_sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
"github.com/kyokomi/lottery" | |
) | |
type Ball struct { | |
Color string | |
Count int | |
} | |
func (b *Ball) Prob() int { | |
return b.Count | |
} | |
func (b *Ball) String() string { | |
return fmt.Sprintf("%s(%d)", b.Color, b.Count) | |
} | |
// 袋の中に球が3種類入っていてそれを順次取り出して行くサンプル | |
func main() { | |
lot := lottery.New(rand.New(rand.NewSource(time.Now().UnixNano()))) | |
red := &Ball{"Red", 1} | |
green := &Ball{"Green", 10} | |
blue := &Ball{"Blue", 3} | |
bag := []lottery.Interface{red, green, blue} | |
for { | |
i := lot.Lots(bag...) | |
if i < 0 { | |
break | |
} | |
b, _ := bag[i].(*Ball) | |
b.Count = b.Count - 1 | |
fmt.Println(b.Color, "--", red, green, blue) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go run main.go | |
Blue -- Red(1) Green(10) Blue(2) | |
Green -- Red(1) Green(9) Blue(2) | |
Blue -- Red(1) Green(9) Blue(1) | |
Green -- Red(1) Green(8) Blue(1) | |
Green -- Red(1) Green(7) Blue(1) | |
Green -- Red(1) Green(6) Blue(1) | |
Green -- Red(1) Green(5) Blue(1) | |
Green -- Red(1) Green(4) Blue(1) | |
Green -- Red(1) Green(3) Blue(1) | |
Blue -- Red(1) Green(3) Blue(0) | |
Red -- Red(0) Green(3) Blue(0) | |
Green -- Red(0) Green(2) Blue(0) | |
Green -- Red(0) Green(1) Blue(0) | |
Green -- Red(0) Green(0) Blue(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment