/
random.go
67 lines (61 loc) · 1.34 KB
/
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
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
package utils
import (
crypto_rand "crypto/rand"
"math/big"
math_rand "math/rand"
"time"
"github.com/0xVanfer/types"
)
// Random from timestamp.
//
// Range:
//
// 0 ~ length-1
func MathRandBelow[T types.Integer](length T) int {
if length == 0 {
return 0
}
s := math_rand.NewSource(time.Now().Unix())
r := math_rand.New(s)
i := r.Intn(int(length))
return i
}
// Real random/ Crypto random.
//
// Range:
//
// 0 ~ length-1
func CryptoRandBelow[T types.Integer](length T) int {
if length == 0 {
return 0
}
n, _ := crypto_rand.Int(crypto_rand.Reader, big.NewInt(int64(length)))
return int(n.Int64())
}
// Choose "times" different numbers between "start" and "end".
//
// Example:
//
// CryptoRandBetween(1, 100, 5) = []int{1, 68, 69, 78, 79, 98, 100}
func CryptoRandBetween(start int, end int, times int) []int {
if end <= start {
return []int{}
}
insert := start
var results []int
results = append(results, insert)
for i := 0; i < times; i++ {
new := CryptoRandBelow(end-start) + start
for ContainInArray(new, results...) {
new = CryptoRandBelow(end-start) + start
}
results = append(results, new)
}
results = append(results, end)
results = SortSimple(true, results...)
return results
}
// Choose an element randomly from the given array.
func CryptoRandFrom[T any](range_ ...T) T {
return range_[CryptoRandBelow(len(range_))]
}