Skip to content

Commit ce5bf51

Browse files
author
hero
committed
添加中奖概率算法
1 parent 9137cb4 commit ce5bf51

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

other/tool_test.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package main
22

33
import (
44
"fmt"
5+
"math/rand"
56
"net"
67
"os"
78
"path/filepath"
89
"testing"
10+
"time"
911
)
1012

1113
//获取本地的IP
@@ -36,14 +38,45 @@ func Test_Path(t *testing.T) {
3638
fmt.Println(appConfigPath)
3739
}
3840

39-
func Test_Defer(t *testing.T) {
40-
defer fmt.Println(1)
41-
A()
41+
func Test_Defer(t *testing.T) {
42+
defer fmt.Println(1)
43+
A()
4244
defer fmt.Println(3)
4345
return
4446
}
45-
func A() {
47+
func A() {
4648
defer func() {
4749
fmt.Println(2)
4850
}()
4951
}
52+
53+
func Test_lottery(t *testing.T) {
54+
var lottery = make(map[string]int)
55+
lottery["特等奖"] = 5
56+
lottery["一等奖"] = 10
57+
lottery["二等奖"] = 35
58+
lottery["三等奖"] = 50
59+
//计算概率
60+
rand.Seed(time.Now().Unix())
61+
var (
62+
randNum int
63+
)
64+
for _, v := range lottery {
65+
randNum += v
66+
}
67+
fmt.Println("从 ", randNum, "中产生随机数")
68+
for j := 0; j < 20; j++ {
69+
i := rand.Intn(randNum)
70+
var (
71+
start int
72+
end int
73+
)
74+
for k, v := range lottery {
75+
end += v
76+
if start <= i && i < end {
77+
fmt.Println("恭喜你中了 ", k)
78+
}
79+
start = end
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)