Documentation
¶
Index ¶
- Constants
- func Charset() string
- func CharsetASCII() string
- func CharsetAlpha() string
- func CharsetDigit() string
- func CheckPowerOfTwo(n int) bool
- func KV[K comparable, V any](mkK func() K, mkV func() V) func() (K, V)
- func Map[K comparable, V any](length int, mk func() (K, V), opts ...mapOption) map[K]V
- func Pick[T any](rnd *Random, vs ...T) T
- func Slice[T any](length int, mk func() T, opts ...sliceOption) []T
- func Unique[T any](blk func() T, excludeList ...T) T
- type Contact
- type CryptoSeed
- type Factory
- type Random
- func (r *Random) Bool() bool
- func (r *Random) Contact(opts ...internal.ContactOption) Contact
- func (r *Random) Domain() string
- func (r *Random) DurationB(min, max time.Duration) time.Duration
- func (r *Random) DurationBetween(min, max time.Duration) time.Duration
- func (r *Random) Email() stringdeprecated
- func (r *Random) Error() error
- func (r *Random) Float32() float32
- func (r *Random) Float64() float64
- func (r *Random) FloatB(min, max float64) float64
- func (r *Random) FloatBetween(min, max float64) float64
- func (r *Random) FloatN(n float64) float64
- func (r *Random) HexN(length int) string
- func (r *Random) Int() int
- func (r *Random) IntB(min, max int) int
- func (r *Random) IntBetween(min, max int) int
- func (r *Random) IntN(n int) int
- func (r *Random) Make(T any) any
- func (r *Random) Name() contactGeneratordeprecated
- func (r *Random) Pick(slice any) any
- func (r *Random) Read(p []byte) (n int, err error)
- func (r *Random) Repeat(min, max int, do func()) int
- func (r *Random) SliceElement(slice any) anydeprecated
- func (r *Random) String() string
- func (r *Random) StringN(length int) string
- func (r *Random) StringNC(length int, charset string) string
- func (r *Random) StringNWithCharset(length int, charset string) string
- func (r *Random) Time() time.Time
- func (r *Random) TimeB(from, to time.Time) time.Time
- func (r *Random) TimeBetween(from, to time.Time) time.Time
- func (r *Random) TimeN(from time.Time, years, months, days int) time.Time
- func (r *Random) UUID() string
Examples ¶
- Map
- Map (WithUniqueValues)
- New
- Pick (PseudoRandomValuePicking)
- Pick (RandomValuePicking)
- Random (CryptoSeed)
- Random (MathRand)
- Random.Bool
- Random.DurationBetween
- Random.Error
- Random.Float32
- Random.Float64
- Random.Int
- Random.IntBetween
- Random.IntN
- Random.Make
- Random.Pick
- Random.Read
- Random.Repeat
- Random.String
- Random.StringN
- Random.StringNC
- Random.StringNWithCharset
- Random.Time
- Random.TimeBetween
- Random.TimeN
- Slice
- Slice (WithUniqueValues)
- Unique
Constants ¶
const UniqueValues = flagUniqueValues(0)
UniqueValues is an option that used to express a desire for unique value generation with certain functions. For example if random.Slice receives the UniqueValues flag, then the created values will be guaranteed to be unique, unless it is not possible within a reasonable attempts using the provided value maker function.
Variables ¶
This section is empty.
Functions ¶
func CharsetASCII ¶
func CharsetASCII() string
func CharsetAlpha ¶
func CharsetAlpha() string
func CharsetDigit ¶
func CharsetDigit() string
func CheckPowerOfTwo ¶ added in v0.179.0
func KV ¶
func KV[K comparable, V any](mkK func() K, mkV func() V) func() (K, V)
func Map ¶
func Map[K comparable, V any](length int, mk func() (K, V), opts ...mapOption) map[K]V
Example ¶
package main import ( "go.llib.dev/testcase/pp" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(random.CryptoSeed{}) m := random.Map[string, int](3, random.KV(rnd.String, rnd.Int)) pp.PP(m) // map[string]int slice with 3 key-value pairs }
Example (WithUniqueValues) ¶
package main import ( "go.llib.dev/testcase/pp" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(random.CryptoSeed{}) m := random.Map[string, int](3, random.KV(rnd.String, rnd.Int), random.UniqueValues) pp.PP(m) // map[string]int slice with 3 key-value pairs }
func Pick ¶ added in v0.146.0
Example (PseudoRandomValuePicking) ¶
package main import ( "math/rand" "go.llib.dev/testcase/random" ) func main() { // Pick pseudo randomly from the given values using the seed. // This will make picking deterministically random when the same seed is used. const seed = 42 rnd := random.New(rand.NewSource(seed)) var _ = random.Pick(rnd, "one", "two", "three") }
Example (RandomValuePicking) ¶
package main import ( "go.llib.dev/testcase/random" ) func main() { // Pick randomly from the values of 1,2,3 var _ = random.Pick(nil, 1, 2, 3) }
func Slice ¶
Example ¶
package main import ( "go.llib.dev/testcase/pp" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(random.CryptoSeed{}) slice := random.Slice[int](3, rnd.Int) pp.PP(slice) // []int slice with 3 values }
Example (WithUniqueValues) ¶
package main import ( "go.llib.dev/testcase/pp" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(random.CryptoSeed{}) slice := random.Slice[int](3, rnd.Int, random.UniqueValues) pp.PP(slice) // []int slice with 3 values }
func Unique ¶ added in v0.145.0
func Unique[T any](blk func() T, excludeList ...T) T
Unique function is a utility that helps with generating distinct values from those in a given exclusion list. If you need multiple unique values of the same type, this helper function can be useful for ensuring they're all different.
rnd := random.New(random.CryptoSeed{}) v1 := random.Unique(rnd.Int) v2 := random.Unique(rnd.Int, v1) v3 := random.Unique(rnd.Int, v1, v2)
Example ¶
package main import ( "testing" "go.llib.dev/testcase/assert" "go.llib.dev/testcase/random" ) var rnd = random.New(random.CryptoSeed{}) func main() { // useful when you need random values which are not equal v1 := rnd.Int() v2 := random.Unique(rnd.Int, v1) v3 := random.Unique(rnd.Int, v1, v2) var tb testing.TB assert.NotEqual(tb, v1, v3) assert.NotEqual(tb, v2, v3) }
Types ¶
type CryptoSeed ¶
type CryptoSeed struct{}
func (CryptoSeed) Int63 ¶
func (c CryptoSeed) Int63() int64
Int63 returns a non-negative pseudo-random 63-bit integer as an int64.
func (CryptoSeed) Seed ¶
func (c CryptoSeed) Seed(seed int64)
Seed should use the provided seed value to initialize the generator to a deterministic state, but in CryptoSeed, the value is ignored.
func (CryptoSeed) Uint64 ¶
func (c CryptoSeed) Uint64() uint64
type Factory ¶
type Factory struct {
// contains filtered or unexported fields
}
func (*Factory) RegisterType ¶
type Random ¶
A Random is a source of random numbers. It is safe to be used in from multiple goroutines.
Example (CryptoSeed) ¶
package main import ( "go.llib.dev/testcase/random" ) func main() { random.New(random.CryptoSeed{}) }
Example (MathRand) ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { seed := time.Now().UnixNano() source := rand.NewSource(seed) random.New(source) }
func New ¶
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { _ = random.New(rand.NewSource(time.Now().Unix())) }
func (*Random) Bool ¶
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.Bool() // returns a random bool }
func (*Random) DurationB ¶ added in v0.173.0
DurationB returns an duration based on the received duration range's [min,max].
func (*Random) DurationBetween ¶ added in v0.164.0
DurationBetween returns an duration based on the received duration range's [min,max].
Example ¶
package main import ( "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(random.CryptoSeed{}) rnd.DurationBetween(time.Second, time.Minute) }
func (*Random) Error ¶
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) err := rnd.Error() _ = err }
func (*Random) Float32 ¶
Float32 returns, as a float32, a pseudo-random number in [0.0,1.0).
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.Float32() }
func (*Random) Float64 ¶
Float64 returns, as a float64, a pseudo-random number in [0.0,1.0).
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.Float64() }
func (*Random) FloatB ¶ added in v0.173.0
FloatB returns a float between the given min and max value range.
func (*Random) FloatBetween ¶ added in v0.173.0
FloatBetween returns a float between the given min and max value range. [min, max]
func (*Random) Int ¶
Int returns a non-negative pseudo-random int.
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.Int() }
func (*Random) IntB ¶
IntB returns, as an int, a non-negative pseudo-random number based on the received int range's [min,max].
func (*Random) IntBetween ¶
IntBetween returns an int based on the received int range's [min,max].
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.IntBetween(24, 42) }
func (*Random) IntN ¶
IntN returns, as an int, a non-negative pseudo-random number in [0,n). It panics if n <= 0.
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.IntN(42) }
func (*Random) Make ¶
Example ¶
package main import ( "go.llib.dev/testcase/random" ) func main() { rnd := random.New(random.CryptoSeed{}) type ExampleStruct struct { Foo string Bar int Baz *int } _ = rnd.Make(&ExampleStruct{}).(*ExampleStruct) // returns a populated struct }
func (*Random) Pick ¶ added in v0.172.0
Pick will return a random element picked from a slice. You need type assert the returned value to get back the original type.
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) // returns a random element from the given slice _ = rnd.Pick([]string{`foo`, `bar`, `baz`}).(string) }
func (*Random) Read ¶
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) p := make([]byte, 42) n, err := rnd.Read(p) _, _ = n, err }
func (*Random) Repeat ¶
Repeat will repeatedly call the "do" function. The number of repeats will be random between the min and the max range. The repeated time will be returned as a result.
Example ¶
package main import ( "go.llib.dev/testcase/random" ) func main() { rnd := random.New(random.CryptoSeed{}) n := rnd.Repeat(1, 3, func() { // will be called repeatedly between 1 and 3 times. }) _ = n // is the number of times, the function block was repeated. }
func (*Random) SliceElement
deprecated
func (*Random) String ¶
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.String() }
func (*Random) StringN ¶
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.StringN(42) }
func (*Random) StringNC ¶
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) rnd.StringNC(42, random.Charset()) rnd.StringNC(42, random.CharsetASCII()) rnd.StringNC(42, random.CharsetAlpha()) rnd.StringNC(42, "ABC") }
func (*Random) StringNWithCharset ¶
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) rnd.StringNWithCharset(42, random.Charset()) rnd.StringNWithCharset(42, random.CharsetASCII()) rnd.StringNWithCharset(42, random.CharsetAlpha()) rnd.StringNWithCharset(42, "ABC") }
func (*Random) Time ¶
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.Time() }
func (*Random) TimeB ¶
TimeB returns, as an time.Time, a non-negative pseudo-random time in [from,to].
func (*Random) TimeBetween ¶
TimeBetween returns, as an time.Time, a non-negative pseudo-random time in [from,to].
Example ¶
package main import ( "math/rand" "time" "go.llib.dev/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.TimeBetween(time.Now(), time.Now().Add(time.Hour)) }