Documentation
¶
Index ¶
- func Charset() string
- func CharsetASCII() string
- func CharsetAlpha() string
- func CharsetDigit() string
- func MakeMap[K comparable, V any](rnd *Random, length int) map[K]V
- func MakeSlice[T any](rnd *Random, length int) []T
- type CryptoSeed
- type Factory
- type Random
- func (r *Random) Bool() bool
- func (r *Random) ElementFromSlice(slice interface{}) interface{}
- func (r *Random) Email() string
- func (r *Random) Error() error
- func (r *Random) Float32() float32
- func (r *Random) Float64() float64
- 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() name
- 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 interface{}) interface{}
- 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 ¶
- MakeMap
- MakeSlice
- New
- Random (CryptoSeed)
- Random (MathRand)
- Random.Bool
- Random.Error
- Random.Float32
- Random.Float64
- Random.Int
- Random.IntBetween
- Random.IntN
- Random.Make
- Random.Read
- Random.Repeat
- Random.SliceElement
- Random.String
- Random.StringN
- Random.StringNC
- Random.StringNWithCharset
- Random.Time
- Random.TimeBetween
- Random.TimeN
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CharsetASCII ¶ added in v0.83.0
func CharsetASCII() string
func CharsetAlpha ¶ added in v0.83.0
func CharsetAlpha() string
func CharsetDigit ¶ added in v0.105.0
func CharsetDigit() string
func MakeMap ¶ added in v0.102.0
func MakeMap[K comparable, V any](rnd *Random, length int) map[K]V
Example ¶
package main import ( "github.com/adamluzsi/testcase/pp" "github.com/adamluzsi/testcase/random" ) func main() { rnd := random.New(random.CryptoSeed{}) pp.PP(random.MakeMap[string, int](rnd, 3)) // map[string]int slice with 3 key-value pairs }
Output:
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 ¶ added in v0.73.0
type Factory struct {
// contains filtered or unexported fields
}
func (*Factory) RegisterType ¶ added in v0.73.0
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 ( "github.com/adamluzsi/testcase/random" ) func main() { random.New(random.CryptoSeed{}) }
Output:
Example (MathRand) ¶
package main import ( "math/rand" "time" "github.com/adamluzsi/testcase/random" ) func main() { seed := time.Now().UnixNano() source := rand.NewSource(seed) random.New(source) }
Output:
func New ¶
Example ¶
package main import ( "math/rand" "time" "github.com/adamluzsi/testcase/random" ) func main() { _ = random.New(rand.NewSource(time.Now().Unix())) }
Output:
func (*Random) Bool ¶
Example ¶
package main import ( "math/rand" "time" "github.com/adamluzsi/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.Bool() // returns a random bool }
Output:
func (*Random) ElementFromSlice ¶
func (r *Random) ElementFromSlice(slice interface{}) interface{}
ElementFromSlice
DEPRECATED: use SliceElement instead
func (*Random) Error ¶ added in v0.90.0
Example ¶
package main import ( "math/rand" "time" "github.com/adamluzsi/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) err := rnd.Error() _ = err }
Output:
func (*Random) Float32 ¶
Float32 returns, as a float32, a pseudo-random number in [0.0,1.0).
Example ¶
package main import ( "math/rand" "time" "github.com/adamluzsi/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.Float32() }
Output:
func (*Random) Float64 ¶
Float64 returns, as a float64, a pseudo-random number in [0.0,1.0).
Example ¶
package main import ( "math/rand" "time" "github.com/adamluzsi/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.Float64() }
Output:
func (*Random) Int ¶
Int returns a non-negative pseudo-random int.
Example ¶
package main import ( "math/rand" "time" "github.com/adamluzsi/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.Int() }
Output:
func (*Random) IntB ¶ added in v0.71.0
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, as an int, a non-negative pseudo-random number based on the received int range's [min,max].
Example ¶
package main import ( "math/rand" "time" "github.com/adamluzsi/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.IntBetween(24, 42) }
Output:
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" "github.com/adamluzsi/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.IntN(42) }
Output:
func (*Random) Make ¶ added in v0.73.0
Example ¶
package main import ( "github.com/adamluzsi/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 }
Output:
func (*Random) Read ¶ added in v0.90.0
Example ¶
package main import ( "math/rand" "time" "github.com/adamluzsi/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) p := make([]byte, 42) n, err := rnd.Read(p) _, _ = n, err }
Output:
func (*Random) Repeat ¶ added in v0.125.0
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 ( "github.com/adamluzsi/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. }
Output:
func (*Random) SliceElement ¶ added in v0.119.0
func (r *Random) SliceElement(slice interface{}) interface{}
SliceElement will return a random slice element. You need type assert the returned value to get back the original type.
Example ¶
package main import ( "math/rand" "time" "github.com/adamluzsi/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) // returns a random element from the given slice _ = rnd.SliceElement([]string{`foo`, `bar`, `baz`}).(string) }
Output:
func (*Random) String ¶
Example ¶
package main import ( "math/rand" "time" "github.com/adamluzsi/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.String() }
Output:
func (*Random) StringN ¶
Example ¶
package main import ( "math/rand" "time" "github.com/adamluzsi/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.StringN(42) }
Output:
func (*Random) StringNC ¶ added in v0.82.0
Example ¶
package main import ( "math/rand" "time" "github.com/adamluzsi/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") }
Output:
func (*Random) StringNWithCharset ¶
Example ¶
package main import ( "math/rand" "time" "github.com/adamluzsi/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") }
Output:
func (*Random) Time ¶
Example ¶
package main import ( "math/rand" "time" "github.com/adamluzsi/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.Time() }
Output:
func (*Random) TimeB ¶ added in v0.71.0
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" "github.com/adamluzsi/testcase/random" ) func main() { rnd := random.New(rand.NewSource(time.Now().Unix())) _ = rnd.TimeBetween(time.Now(), time.Now().Add(time.Hour)) }
Output: