random

package
v0.44.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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 Random

type Random struct {
	Source rand.Source
	// contains filtered or unexported fields
}

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

func New(s rand.Source) *Random

func (*Random) Bool

func (r *Random) Bool() 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{}
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.ElementFromSlice([]string{`foo`, `bar`, `baz`}).(string)
}
Output:

func (*Random) Float32

func (r *Random) Float32() 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

func (r *Random) Float64() 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

func (r *Random) Int() 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) IntBetween

func (r *Random) IntBetween(min, max int) int

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

func (r *Random) IntN(n int) int

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) KeyFromMap

func (r *Random) KeyFromMap(anyMap interface{}) interface{}
Example
package main

import (
	"math/rand"
	"time"

	"github.com/adamluzsi/testcase/random"
)

func main() {
	rnd := random.New(rand.NewSource(time.Now().Unix()))

	_ = rnd.KeyFromMap(map[string]struct{}{
		`foo`: {},
		`bar`: {},
		`baz`: {},
	}).(string)
}
Output:

func (*Random) String

func (r *Random) String() 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

func (r *Random) StringN(length int) string
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) StringNWithCharset

func (r *Random) StringNWithCharset(length int, charset string) string

func (*Random) Time

func (r *Random) Time() time.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) TimeBetween

func (r *Random) TimeBetween(from, to time.Time) time.Time

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:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL