forked from jnovikov/diceroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (62 loc) · 1.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"flag"
"fmt"
"math/rand"
"time"
)
var (
seed = flag.Int("seed", 0, "seed for random generator. unix(now) be default")
start = flag.Int("start", 1, "left default 1")
end = flag.Int("end", 6, "right default 6")
n = flag.Int("n", 1, "round count default 1")
norepeat = flag.Bool("norepeat", false, "no repeat numbers")
)
// Фукнция должна вернуть число из интервала [l,r]
func randInterval(l, r int) int {
return l + rand.Intn(r+1)
}
func main() {
flag.Parse()
if *start > *end {
fmt.Println("start > end")
return
}
if *seed == 0 {
rand.Seed(time.Now().Unix())
} else {
rand.Seed(int64(*seed))
}
if *norepeat {
length := *end - *start
if *n > length {
fmt.Println("numbers count more than length")
return
} else {
var result []int
step := 0
for step < *n {
tmp := randInterval(*start, *end)
found := false
for _, value := range result {
if tmp == value {
found = true
break
}
}
if !found {
result = append(result, tmp)
step++
}
}
for _, value := range result {
fmt.Print(value)
fmt.Print(" ")
}
}
} else {
for i := 0; i < *n; i++ {
fmt.Println(randInterval(*start, *end))
}
}
}