xrand

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package xrand contains extensions to the standard library package math/rand.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RSample

func RSample(r *rand.Rand, n int, k int) []int

RSample pseudo-randomly picks k ints uniformly without replacement from [0, n).

If n < k, returns all ints in [0, n).

Requires O(k) time and space.

func RSampleIterator

func RSampleIterator[T any](r *rand.Rand, iter iterator.Iterator[T], k int) []T

RSampleIterator pseudo-randomly picks k items uniformly without replacement from iter.

If iter yields fewer than k items, returns all of them.

Uses a reservoir sample (https://en.wikipedia.org/wiki/Reservoir_sampling), which uses time linear in the length of iter but only O(k) extra space.

func RSampleSlice

func RSampleSlice[T any](r *rand.Rand, a []T, k int) []T

RSampleSlice pseudo-randomly picks k items uniformly without replacement from a.

If len(a) < k, returns all items in a.

Uses a reservoir sample (https://en.wikipedia.org/wiki/Reservoir_sampling), which uses O(k) time and space.

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.

Uses a reservoir sample (https://en.wikipedia.org/wiki/Reservoir_sampling), which uses time linear in the length of s but only O(k) extra space.

func RShuffle

func RShuffle[T any](r *rand.Rand, a []T)

RShuffle pseudo-randomizes the order of a.

func Sample

func Sample(n int, k int) []int

Sample pseudo-randomly picks k ints uniformly without replacement from [0, n).

If n < k, returns all ints in [0, n).

Requires O(k) time and space.

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)

	for _, x := range sample {
		fmt.Println(x)
	}

}
Output:

45
71
88
93
60

func SampleIterator

func SampleIterator[T any](iter iterator.Iterator[T], k int) []T

SampleIterator pseudo-randomly picks k items uniformly without replacement from iter.

If iter yields fewer than k items, returns all of them.

Uses a reservoir sample (https://en.wikipedia.org/wiki/Reservoir_sampling), which uses time linear in the length of iter but only O(k) extra space.

func SampleSlice

func SampleSlice[T any](a []T, k int) []T

SampleSlice pseudo-randomly picks k items uniformly without replacement from a.

If len(a) < k, returns all items in a.

Uses a reservoir sample (https://en.wikipedia.org/wiki/Reservoir_sampling), which uses O(k) time and space.

func SampleStream

func SampleStream[T any](ctx context.Context, s stream.Stream[T], k int) ([]T, error)

SampleStream pseudo-randomly picks k items uniformly without replacement from s.

If s yields fewer than k items, returns all of them.

Uses a reservoir sample (https://en.wikipedia.org/wiki/Reservoir_sampling), which uses time linear in the length of s but only O(k) extra space.

func Shuffle

func Shuffle[T any](a []T)

Shuffle pseudo-randomizes the order of a.

Types

This section is empty.

Jump to

Keyboard shortcuts

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