shuffle

package module
v0.0.0-...-b0f7234 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2016 License: MIT Imports: 2 Imported by: 0

README

shuffle

GoDocBuild Status

Shuffle randomizes data within slices using the Fisher-Yates shuffle algorithm.

Shuffle has implementations using both a PCG PRNG and crypto/rand. Use the one that fits your use case. The PRNG implementations can be found in shuffle/quick.

String and Numeric types

Shuffles are implemented for slices of Go bytes, strings, and numeric types. These shuffles modify the received slice; no additional slice related allocations will occur.

Shuffler interface

Other types can be shuffled by implementing the shuffler interface and calling either shuffle.Interface(c Shuffler), for crypto/rand based shuffles, or quick.Shuffle(c shuffle.Shuffler) for PRNG based shuffle.

See bench_test.go's Deck for an example implementation.

shuffle

Shuffles the elements using crypto/rand.

shuffle/quick

Quick shuffles allocate minimal memory, if any, and are much faster than crypto/rand shuffles. The slices are modified in place and nothing is returned. The PRNG is seeded using a value obtained from crypto/rand during initialization; if this operation results in an error, a panic will occur.

License

Copyright 2015-16 Joel Scoble, all rights reserved.

Licensed under The MIT License. Please see the included license file for more details.

Documentation

Overview

Shuffle provides crypto/rand based shuffling (randomization) of collections using the Fisher-Yates (Knuth) shuffling algorithm. Functions for shuffling slices of non-composite types are provided, or you can implement the Shuffler interface and shuffle using the shuffle.Interface() func.

Shuffling is performed on the received slice; nothing is returned and no additional allocations, aside from those caused by using crypto/rand.

If using a math/rand based shuffle is good enough, see the github.com/mohae/shuffle/quick package, which provides a quick, pseudo-random shuffle.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Byte

func Byte(c []byte) error

Byte randomizes a byte slice.

func Complex128

func Complex128(c []complex128) error

Complex129 randomizes a complex128 slice.

func Complex64

func Complex64(c []complex64) error

Complex64 randomizes a complex64 slice.

func Float32

func Float32(c []float32) error

Float32 randomizes a float32 slice.

func Float64

func Float64(c []float64) error

Float64 randomizes a float64 slice.

func Int

func Int(c []int) error

Int randomizes an int slice.

func Int16

func Int16(c []int16) error

Int16 randomizes an int16 slice.

func Int32

func Int32(c []int32) error

Int32 randomizes an int32 slice.

func Int64

func Int64(c []int64) error

Int64 randomizes an int64 slice.

func Int8

func Int8(c []int8) error

Int8 randomizes an int8 slice.

func Interface

func Interface(c Shuffler) error

Shuffle randomizes collections: use this for Shuffler implementations.

func String

func String(c []string) error

String randomizes a strings slice.

func Uint

func Uint(c []uint) error

Uint randomizes an uint slice.

func Uint16

func Uint16(c []uint16) error

Uint16 randomizes an uint16 slice.

func Uint32

func Uint32(c []uint32) error

Uint32 randomizes an uint32 slice.

func Uint64

func Uint64(c []uint64) error

Uint64 randomizes an uint64 slice.

func Uint8

func Uint8(c []uint8) error

Uint8 randomizes an uint8 slice.

Types

type Shuffler

type Shuffler interface {
	Len() int      // number of elements in the p
	Swap(i, j int) // swaps elements
}

Shuffler interface.

Directories

Path Synopsis
Shuffle provides math/rand based shuffling (randomization) of collections using the Fisher-Yates (Knuth) shuffling algorithm.
Shuffle provides math/rand based shuffling (randomization) of collections using the Fisher-Yates (Knuth) shuffling algorithm.

Jump to

Keyboard shortcuts

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