Documentation
¶
Overview ¶
Package xrand contains extensions to the standard library package math/rand.
Index ¶
- func RSample(r *rand.Rand, n int, k int) []int
- func RSampleIterator[T any](r *rand.Rand, iter iterator.Iterator[T], k int) []T
- func RSampleSlice[T any](r *rand.Rand, a []T, k int) []T
- func RSampleStream[T any](ctx context.Context, r *rand.Rand, s stream.Stream[T], k int) ([]T, error)
- func RShuffle[T any](r *rand.Rand, a []T)
- func Sample(n int, k int) []int
- func SampleIterator[T any](iter iterator.Iterator[T], k int) []T
- func SampleSlice[T any](a []T, k int) []T
- func SampleStream[T any](ctx context.Context, s stream.Stream[T], k int) ([]T, error)
- func Shuffle[T any](a []T)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RSample ¶
RSample pseudo-randomly picks k ints uniformly without replacement from [0, n).
If n < k, returns all ints in [0, n).
The output is not in any particular order. If a pseudo-random order is desired, the output should be passed to Shuffle.
func RSampleIterator ¶
RSampleIterator pseudo-randomly picks k items uniformly without replacement from iter.
If iter yields fewer than k items, returns all of them.
The output is not in any particular order. If a pseudo-random order is desired, the output should be passed to Shuffle.
func RSampleSlice ¶
RSampleSlice pseudo-randomly picks k items uniformly without replacement from a.
If len(a) < k, returns all items in a.
The output is not in any particular order. If a pseudo-random order is desired, the output should be passed to Shuffle.
func RSampleStream ¶
func RSampleStream[T any]( ctx context.Context, r *rand.Rand, s stream.Stream[T], k int, ) ([]T, error)
RSampleStream pseudo-randomly picks k items uniformly without replacement from s.
If s yields fewer than k items, returns all of them.
The output is not in any particular order. If a pseudo-random order is desired, the output should be passed to Shuffle.
func Sample ¶
Sample pseudo-randomly picks k ints uniformly without replacement from [0, n).
If n < k, returns all ints in [0, n).
The output is not in any particular order. If a pseudo-random order is desired, the output should be passed to Shuffle.
Example ¶
package main
import (
"fmt"
"math/rand"
"github.com/bradenaw/juniper/xmath/xrand"
)
func main() {
r := rand.New(rand.NewSource(0))
sample := xrand.RSample(r, 100, 5)
fmt.Println(sample)
}
Output: [45 71 88 93 60]
func SampleIterator ¶
SampleIterator pseudo-randomly picks k items uniformly without replacement from iter.
If iter yields fewer than k items, returns all of them.
The output is not in any particular order. If a pseudo-random order is desired, the output should be passed to Shuffle.
func SampleSlice ¶
SampleSlice pseudo-randomly picks k items uniformly without replacement from a.
If len(a) < k, returns all items in a.
The output is not in any particular order. If a pseudo-random order is desired, the output should be passed to Shuffle.
func SampleStream ¶
SampleStream pseudo-randomly picks k items uniformly without replacement from s.
If s yields fewer than k items, returns all of them.
The output is not in any particular order. If a pseudo-random order is desired, the output should be passed to Shuffle.
Types ¶
This section is empty.