diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-02-05 00:37:32 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-02-05 00:37:32 +0100 |
| commit | 6960aecc25400320adee1b8802a86839326e15b6 (patch) | |
| tree | 334f7ca9491080a5e6f9a9747da77281c4958ba2 /vendor/github.com/go-faker/faker/v4/random_source.go | |
| download | hepi-6960aecc25400320adee1b8802a86839326e15b6.tar.gz | |
Engage!
Diffstat (limited to 'vendor/github.com/go-faker/faker/v4/random_source.go')
| -rw-r--r-- | vendor/github.com/go-faker/faker/v4/random_source.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/vendor/github.com/go-faker/faker/v4/random_source.go b/vendor/github.com/go-faker/faker/v4/random_source.go new file mode 100644 index 0000000..96b7a15 --- /dev/null +++ b/vendor/github.com/go-faker/faker/v4/random_source.go @@ -0,0 +1,49 @@ +package faker + +import ( + "io" + mathrand "math/rand" + "sync" +) + +var ( + rand *mathrand.Rand + crypto io.Reader +) + +type safeSource struct { + mx sync.Mutex + mathrand.Source +} + +func (s *safeSource) Int63() int64 { + s.mx.Lock() + defer s.mx.Unlock() + + return s.Source.Int63() +} + +// NewSafeSource wraps an unsafe rand.Source with a mutex to guard the random source +// against concurrent access. +func NewSafeSource(in mathrand.Source) mathrand.Source { + return &safeSource{ + Source: in, + } +} + +// SetRandomSource sets a new random source at the package level. +// +// To use a concurrent-safe source, you may wrap it with NewSafeSource, +// e.g. SetRandomSource(NewSafeSource(mysource)). +// +// The default is the global, concurrent-safe source provided by math/rand. +func SetRandomSource(in mathrand.Source) { + rand = mathrand.New(in) +} + +// SetCryptoSource sets a new reader for functions using a cryptographically-safe random generator (e.g. UUID). +// +// The default is the global source provided by crypto/rand. +func SetCryptoSource(in io.Reader) { + crypto = in +} |
