xrand

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 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).

The output is not in any particular order. If a pseudo-random order is desired, the output should be passed to Shuffle.

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.

The output is not in any particular order. If a pseudo-random order is desired, the output should be passed to Shuffle.

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.

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

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

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.

The output is not in any particular order. If a pseudo-random order is desired, the output should be passed to Shuffle.

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.

The output is not in any particular order. If a pseudo-random order is desired, the output should be passed to Shuffle.

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.

The output is not in any particular order. If a pseudo-random order is desired, the output should be passed to Shuffle.

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