-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.go
204 lines (190 loc) · 6.73 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package unify4g
import (
"math/rand"
"time"
)
var r *rand.Rand // Package-level random generator
func init() {
// Initialize the package-level random generator with a seed
src := rand.NewSource(time.Now().UTC().UnixNano())
r = rand.New(src)
}
// NextInt generates a random integer within the specified range, inclusive.
//
// The function takes two parameters, `min` and `max`, which define the lower and upper bounds of the range.
// It checks if `min` is greater than or equal to `max`, in which case it returns `min` as a default value.
// This behavior may need to be adjusted based on how you want to handle such cases (e.g., returning an error or panic).
//
// The function uses a package-level random generator to generate a random integer in the range from `min` to `max`,
// ensuring that both bounds are included in the result. The formula used is:
// r.Intn(max-min+1) + min, where `r` is a custom random generator that has been initialized elsewhere in the code.
//
// Parameters:
// - `min`: The lower bound of the random number range (inclusive).
// - `max`: The upper bound of the random number range (inclusive).
//
// Returns:
// - A random integer between `min` and `max`, including both bounds.
//
// Example:
//
// randomNum := NextInt(1, 10)
// fmt.Println("Random number between 1 and 10:", randomNum)
func NextInt(min, max int) int {
if min >= max {
return min // or handle the error appropriately
}
return r.Intn(max-min+1) + min // Ensure the range includes max
}
// NextReseed generates a random integer within the specified range, inclusive,
// and reseeds the random number generator with a new value each time it is called.
//
// The function first creates a new seed by combining the current UTC time in nanoseconds
// with a random integer from the custom random generator `r`. This helps to ensure that
// each call to `NextReseed` produces a different sequence of random numbers.
//
// If the provided `min` is greater than or equal to `max`, the function returns `min`
// as a default value. This behavior should be considered when using this function,
// as it may not be intuitive to return `min` in cases where the range is invalid.
//
// The function uses the reseeded random generator to generate a random integer in the
// inclusive range from `min` to `max`. The calculation ensures that both bounds are included
// in the result by using the formula: r.Intn(max-min+1) + min.
//
// Parameters:
// - `min`: The lower bound of the random number range (inclusive).
// - `max`: The upper bound of the random number range (inclusive).
//
// Returns:
// - A random integer between `min` and `max`, including both bounds.
//
// Example:
//
// randomNum := NextReseed(1, 10)
// fmt.Println("Random number between 1 and 10 after reseeding:", randomNum)
func NextReseed(min, max int) int {
// Reseed the custom random generator with a new seed
x := time.Now().UTC().UnixNano() + int64(r.Int())
r.Seed(x)
// Ensure max is included in the range
if min >= max {
return min
}
return r.Intn(max-min+1) + min
}
// NextUUID generates and returns a new UUID using the GenerateUUID function.
//
// If an error occurs during UUID generation (for example, if there is an issue reading from /dev/urandom),
// the function returns an empty string.
//
// This function is useful when you want a simple UUID generation without handling errors directly.
// It abstracts away the error handling by returning an empty string in case of failure.
//
// Returns:
// - A string representing the newly generated UUID.
// - An empty string if an error occurs during UUID generation.
//
// Example:
//
// uuid := NextUUID()
//
// if uuid == "" {
// fmt.Println("Failed to generate UUID")
// } else {
//
// fmt.Println("Generated UUID:", uuid)
// }
func NextUUID() string {
uuid, err := GenerateUUID()
if err != nil {
return ""
}
return uuid
}
// NextFloat64 returns the next random float64 value in the range [0.0, 1.0).
//
// This function uses the rand package to generate a random float64 value.
// The generated value is uniformly distributed over the interval [0.0, 1.0).
//
// Returns:
// - A random float64 value between 0.0 and 1.0.
func NextFloat64() float64 {
return rand.Float64()
}
// NextFloat64Bounded returns the next random float64 value bounded by the specified range.
//
// Parameters:
// - `start`: The lower bound of the random float64 value (inclusive).
// - `end`: The upper bound of the random float64 value (exclusive).
//
// Returns:
// - A random float64 value uniformly distributed between `start` and `end`.
func NextFloat64Bounded(start float64, end float64) float64 {
return rand.Float64()*(end-start) + start
}
// NextFloat32 returns the next random float32 value in the range [0.0, 1.0).
//
// This function uses the rand package to generate a random float32 value.
// The generated value is uniformly distributed over the interval [0.0, 1.0).
//
// Returns:
// - A random float32 value between 0.0 and 1.0.
func NextFloat32() float32 {
return rand.Float32()
}
// NextFloat32Bounded returns the next random float32 value bounded by the specified range.
//
// Parameters:
// - `start`: The lower bound of the random float32 value (inclusive).
// - `end`: The upper bound of the random float32 value (exclusive).
//
// Returns:
// - A random float32 value uniformly distributed between `start` and `end`.
func NextFloat32Bounded(start float32, end float32) float32 {
return rand.Float32()*(end-start) + start
}
// NextIntBounded returns the next random int value bounded by the specified range.
//
// Parameters:
// - `start`: The lower bound of the random int value (inclusive).
// - `end`: The upper bound of the random int value (exclusive).
//
// Returns:
// - A random int value uniformly distributed between `start` and `end`.
func NextIntBounded(start int, end int) int {
return start + rand.Intn(end-start)
}
// NextIntUpperBounded returns the next random int value bounded by a maximum value.
//
// Parameters:
// - `end`: The exclusive upper bound for the random int value.
//
// Returns:
// - A random int value uniformly distributed in the range [0, end).
func NextIntUpperBounded(end int) int {
return rand.Intn(end)
}
// NextBytes creates an array of random bytes with the specified length.
//
// Parameters:
// - `count`: The number of random bytes to generate.
//
// Returns:
// - A slice of random bytes with the specified length.
func NextBytes(count int) []byte {
a := make([]byte, count)
for i := range a {
a[i] = (byte)(nextInt())
}
return a
}
// nextInt returns the next random int value.
//
// This helper function generates a random int value using the rand package.
// It is called by NextBytes to populate the byte array with random values.
//
// Returns:
// - A random int value.
func nextInt() int {
return rand.Int()
}