sampler

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

README

Go Reference

sampler

Sampler is a golang library for sampling from a slice of items.

Install

go get github.com/ninedraft/sampler

License

APACHE 2.0

Documentation

Overview

Package sampler is a golang library for sampling from a slice of items.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Choice

func Choice[E any](rnd Rand, items []E) E

Choice returns a random item from items. If items is empty, Choice panics.

Example
rnd := newFixedRnd()
items := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
choice := sampler.Choice(rnd, items)

fmt.Println(choice)
Output:

2

func Choices

func Choices[E any](rnd Rand, items []E, n int) []E

Choices returns a random selection of n items from items. Items are not guaranteed to be unique.

Example
rnd := newFixedRnd()

items := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
choices := sampler.Choices(rnd, items, 3)

fmt.Println(choices)
Output:

[2 1 3]

func ChoicesAppend

func ChoicesAppend[E any](rnd Rand, dst, items []E, n int) []E

ChoicesAppend returns a random selection of n items from items. Items are appended to dst, which is grown if necessary. Items are not guaranteed to be unique.

func Sample

func Sample[E any](rnd Rand, items []E, n int) []E

Sample returns a random selection of n items from items. Items with unique indexes are selected.

[1 2 3 4 5] -> always unique items in result
[1 2 3 4 4] -> 4 can be selected twice
Example
rnd := newFixedRnd()

items := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
sample := sampler.Sample(rnd, items, 3)

fmt.Println(sample)
Output:

[2 1 3]

func SampleAppend

func SampleAppend[E any](rnd Rand, dst, items []E, n int) []E

SampleAppend returns a random selection of n items from items. Items are appended to dst, which is grown if necessary. Items with unique indexes are selected.

[1 2 3 4 5] -> always unique items in result
[1 2 3 4 4] -> 4 can be selected twice

Types

type Rand

type Rand interface {
	IntN(n int) int
}

Rand is a random number generator.

Jump to

Keyboard shortcuts

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