-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandutil.go
More file actions
86 lines (73 loc) · 2.3 KB
/
randutil.go
File metadata and controls
86 lines (73 loc) · 2.3 KB
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
package randutil
import (
"github.com/aatuh/randutil/v2/collection"
"github.com/aatuh/randutil/v2/core"
"github.com/aatuh/randutil/v2/dist"
"github.com/aatuh/randutil/v2/email"
"github.com/aatuh/randutil/v2/numeric"
"github.com/aatuh/randutil/v2/randstring"
"github.com/aatuh/randutil/v2/randtime"
"github.com/aatuh/randutil/v2/uuid"
)
// Rand provides access to generators from the subpackages, bound to a single
// entropy source. This eliminates the need to duplicate all the methods from
// each subpackage.
type Rand struct {
// Core provides basic random number generation primitives.
Core *core.Generator
// Numeric provides numeric helpers (ranges, ints, bytes).
Numeric *numeric.Generator
// Dist provides statistical distributions.
Dist *dist.Generator
// String provides random string and token generation.
String *randstring.Generator
// UUID provides UUID v4 and v7 generation.
UUID *uuid.Generator
// Time provides random datetime generation.
Time *randtime.Generator
// Email provides random email address generation.
Email *email.Generator
}
// New returns a Rand with all generators bound to src. Pass nil to use
// crypto/rand.
//
// Parameters:
// - src: The entropy source to use.
//
// Returns:
// - Rand: A new Rand with all generators bound to src.
func New(src core.Source) Rand {
coreGen := core.New(src)
return Rand{
Core: coreGen,
Numeric: numeric.New(coreGen),
Dist: dist.New(coreGen),
String: randstring.New(coreGen),
UUID: uuid.New(coreGen),
Time: randtime.New(coreGen),
Email: email.New(coreGen),
}
}
// Default returns a Rand using crypto/rand.
//
// Returns:
// - Rand: A new Rand using crypto/rand.
func Default() Rand { return New(nil) }
// Secure is an alias for Default, provided for clarity when the
// caller wants to emphasize security properties.
//
// Returns:
// - Rand: A new Rand using crypto/rand.
func Secure() Rand { return Default() }
// Source exposes the underlying entropy source.
//
// Returns:
// - core.Source: The underlying entropy source.
func (r Rand) Source() core.Source { return r.Core.Source() }
// Collection returns a collection generator bound to this Rand's RNG.
func Collection[T any](r Rand) *collection.Generator[T] {
if r.Core == nil {
return collection.New[T](nil)
}
return collection.New[T](r.Core)
}