forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.go
37 lines (32 loc) · 825 Bytes
/
random.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
package mathutil
import (
"math/rand"
"time"
)
// RandomInt return a random int at the [min, max)
//
// Usage:
//
// RandomInt(10, 99)
// RandomInt(100, 999)
// RandomInt(1000, 9999)
func RandomInt(min, max int) int {
rr := rand.New(rand.NewSource(time.Now().UnixNano()))
return min + rr.Intn(max-min)
}
// RandInt alias of RandomInt()
func RandInt(min, max int) int { return RandomInt(min, max) }
// RandIntWithSeed alias of RandomIntWithSeed()
func RandIntWithSeed(min, max int, seed int64) int {
return RandomIntWithSeed(min, max, seed)
}
// RandomIntWithSeed return a random int at the [min, max)
//
// Usage:
//
// seed := time.Now().UnixNano()
// RandomIntWithSeed(1000, 9999, seed)
func RandomIntWithSeed(min, max int, seed int64) int {
rr := rand.New(rand.NewSource(seed))
return min + rr.Intn(max-min)
}