Documentation ¶
Overview ¶
Package sample implements sampling algorithms for 1 in n sampling for a random value (probe):
- Modulo, using modulo-operation
- PowerOf2, using bitwise AND-operation, only usable if the sampling rate is a power of 2
- LowerThan, checking if the probe is lower than a pre calculated boundary (maximum value for probe divided by sampling rate)
- Reciprocal, using a multiplication by the reciprocal value of the sampling rate (Details: https://breml.github.io/blog/2015/10/22/dividable-without-remainder/)
- Decrement, implementation variant, where the random value is only calculated after a successful sampling
Index ¶
- func Deviation(state State) (deviation float64)
- func Stats(state State) string
- type Sampler
- func NewDecrement(rate uint64) (Sampler, error)
- func NewDecrementSeeded(rate uint64, seed int64) (Sampler, error)
- func NewLessThan(rate uint64) (Sampler, error)
- func NewLessThanSeeded(rate uint64, seed int64) (Sampler, error)
- func NewModulo(rate uint64) (Sampler, error)
- func NewModuloSeeded(rate uint64, seed int64) (Sampler, error)
- func NewPowerOf2(rate uint64) (Sampler, error)
- func NewPowerOf2Seeded(rate uint64, seed int64) (Sampler, error)
- func NewReciprocalUint16(rate uint64) (Sampler, error)
- func NewReciprocalUint16Seeded(rate uint64, seed int64) (Sampler, error)
- func NewReciprocalUint32(rate uint64) (Sampler, error)
- func NewReciprocalUint32Seeded(rate uint64, seed int64) (Sampler, error)
- func NewReciprocalUint64(rate uint64) (Sampler, error)
- func NewReciprocalUint64Seeded(rate uint64, seed int64) (Sampler, error)
- func NewReciprocalUint8(rate uint64) (Sampler, error)
- func NewReciprocalUint8Seeded(rate uint64, seed int64) (Sampler, error)
- type State
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Sampler ¶
type Sampler interface { // Sample returns a sampling decision based on a random number. Sample() bool // SampleFrom returns a sampling decision based on probe. SampleFrom(probe uint64) bool State }
A Sampler is a source of sampling decisions.
func NewDecrement ¶
NewDecrement returns a Decrement Sampler, random number generator is seeded with UnixNano
func NewDecrementSeeded ¶
NewDecrementSeeded returns a Decrement Sampler, allow for manual seeding of the random number generator
Example ¶
s, err := NewDecrementSeeded(10, 0) if err != nil { fmt.Println("Unable to initialize sampler", err) } for i := 0; i < 100; i++ { if s.Sample() { fmt.Println(i, "got sampled by Decrement sampler") } } fmt.Println(Stats(s))
Output: 5 got sampled by Decrement sampler 23 got sampled by Decrement sampler 31 got sampled by Decrement sampler 37 got sampled by Decrement sampler 45 got sampled by Decrement sampler 63 got sampled by Decrement sampler 68 got sampled by Decrement sampler 79 got sampled by Decrement sampler 87 got sampled by Decrement sampler Rate: 10, SampleCount: 100, TrueCount: 9, Deviation: -11.1111%
func NewLessThan ¶
NewLessThan returns a LessThan Sampler, random number generator is seeded with UnixNano
func NewLessThanSeeded ¶
NewLessThanSeeded returns a LessThan Sampler, allow for manual seeding of the random number generator
Example ¶
s, err := NewLessThanSeeded(10, 0) if err != nil { fmt.Println("Unable to initialize sampler", err) } for i := 0; i < 100; i++ { if s.Sample() { fmt.Println(i, "got sampled by LessThan sampler") } } fmt.Println(Stats(s))
Output: 25 got sampled by LessThan sampler 27 got sampled by LessThan sampler 29 got sampled by LessThan sampler 43 got sampled by LessThan sampler 54 got sampled by LessThan sampler 73 got sampled by LessThan sampler Rate: 10, SampleCount: 100, TrueCount: 6, Deviation: -66.6667%
func NewModulo ¶
NewModulo returns a Modulo Sampler, random number generator is seeded with UnixNano
func NewModuloSeeded ¶
NewModuloSeeded returns a Modulo Sampler, allow for manual seeding of the random number generator
Example ¶
s, err := NewModuloSeeded(10, 0) if err != nil { fmt.Println("Unable to initialize sampler", err) } for i := 0; i < 100; i++ { if s.Sample() { fmt.Println(i, "got sampled by Modulo sampler") } } fmt.Println(Stats(s))
Output: 12 got sampled by Modulo sampler 31 got sampled by Modulo sampler 34 got sampled by Modulo sampler 60 got sampled by Modulo sampler Rate: 10, SampleCount: 100, TrueCount: 4, Deviation: -150.0000%
func NewPowerOf2 ¶
NewPowerOf2 returns a PowerOf2 Sampler, random number generator is seeded with UnixNano
func NewPowerOf2Seeded ¶
NewPowerOf2Seeded returns a PowerOf2 Sampler, allow for manual seeding of the random number generator
Example ¶
s, err := NewPowerOf2Seeded(16, 0) if err != nil { fmt.Println("Unable to initialize sampler", err) } for i := 0; i < 100; i++ { if s.Sample() { fmt.Println(i, "got sampled by PowerOf2 sampler") } } fmt.Println(Stats(s))
Output: 26 got sampled by PowerOf2 sampler 45 got sampled by PowerOf2 sampler Rate: 16, SampleCount: 100, TrueCount: 2, Deviation: -212.5000%
func NewReciprocalUint16 ¶
NewReciprocalUint16 returns a Modulo Sampler, random number generator is seeded with UnixNano
func NewReciprocalUint16Seeded ¶
NewReciprocalUint16Seeded returns a ReciprocalUint16 Sampler, allow for manual seeding of the random number generator
Example ¶
s, err := NewReciprocalUint16Seeded(10, 0) if err != nil { fmt.Println("Unable to initialize sampler", err) } for i := 0; i < 100; i++ { if s.Sample() { fmt.Println(i, "got sampled by ReciprocalUint16 sampler") } } fmt.Println(Stats(s))
Output:
func NewReciprocalUint32 ¶
NewReciprocalUint32 returns a Modulo Sampler, random number generator is seeded with UnixNano
func NewReciprocalUint32Seeded ¶
NewReciprocalUint32Seeded returns a ReciprocalUint32 Sampler, allow for manual seeding of the random number generator
Example ¶
s, err := NewReciprocalUint32Seeded(10, 0) if err != nil { fmt.Println("Unable to initialize sampler", err) } for i := 0; i < 100; i++ { if s.Sample() { fmt.Println(i, "got sampled by ReciprocalUint32 sampler") } } fmt.Println(Stats(s))
Output:
func NewReciprocalUint64 ¶
NewReciprocalUint64 returns a Modulo Sampler, random number generator is seeded with UnixNano
func NewReciprocalUint64Seeded ¶
NewReciprocalUint64Seeded returns a ReciprocalUint64 Sampler, allow for manual seeding of the random number generator
Example ¶
s, err := NewReciprocalUint64Seeded(10, 0) if err != nil { fmt.Println("Unable to initialize sampler", err) } for i := 0; i < 100; i++ { if s.Sample() { fmt.Println(i, "got sampled by ReciprocalUint64 sampler") } } fmt.Println(Stats(s))
Output:
func NewReciprocalUint8 ¶
NewReciprocalUint8 returns a Modulo Sampler, random number generator is seeded with UnixNano
func NewReciprocalUint8Seeded ¶
NewReciprocalUint8Seeded returns a ReciprocalUint8 Sampler, allow for manual seeding of the random number generator
Example ¶
s, err := NewReciprocalUint8Seeded(10, 0) if err != nil { fmt.Println("Unable to initialize sampler", err) } for i := 0; i < 100; i++ { if s.Sample() { fmt.Println(i, "got sampled by ReciprocalUint8 sampler") } } fmt.Println(Stats(s))
Output:
type State ¶
type State interface { // Reset the internal state of the sampler to the initial values // This also resets the random number generator to the initial state with the initial seed value Reset() // String returns the internal state of the sampler as string String() string // Returns the rate Rate() uint64 // Calls returns how many times the sampler was asked to sample Calls() uint64 // Count returns how many times the sampler sampled (returned true) Count() uint64 }
A State is the internal State of a Sampler.