shuffle

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2024 License: MIT Imports: 1 Imported by: 0

README

go-shuffle

test Go Reference

Package shuffle provides primitives for shuffling slices and user-defined collections.

import "github.com/shogo82148/go-shuffle"

// Shuffle slice of int.
ints := []int{3, 1, 4, 1, 5, 9}
shuffle.Ints(ints)

// Shuffle slice of int64.
int64s := []int64{3, 1, 4, 1, 5, 9}
shuffle.Int64s(int64s)

// Shuffle slice of string.
strings := []string{"foo", "bar"}
shuffle.Strings(strings)

// Shuffle slice of float64.
float64s := []float64{3, 1, 4, 1, 5, 9}
shuffle.Float64s(float64s)

// Shuffle slices
shuffle.Slice(ints)
shuffle.Slice(int64s)
shuffle.Slice(strings)
shuffle.Slice(float64s)

See godoc for more information.

LICENSE

This software is released under the MIT License, see LICENSE.md.

Documentation

Overview

Package shuffle provides primitives for shuffling slices and user-defined collections.

As of Go 1.10, the same functionality is now provided by package math/rand, and those implementations should be preferred in new code.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Float64s

func Float64s(a []float64)

Float64s shuffles a slice of float64s.

func Ints

func Ints(a []int)

Ints shuffles a slice of ints.

Example
x := []int{1, 2, 3, 4, 5}
shuffle.Ints(x)
for _, value := range x {
	fmt.Println(value)
	
Output:
1
2
3
4
5

func Shuffle

func Shuffle(data Interface)

Shuffle shuffles Data.

As of Go 1.10, it just calls rand.Shuffle(data.Len(), data.Swap).

func Slice

func Slice[T any](slice []T)

Slice shuffles the slice.

func Strings

func Strings(a []string)

Strings shuffles a slice of strings.

Types

type Interface

type Interface interface {
	// Len is the number of elements in the collection.
	Len() int
	// Swap swaps the elements with indexes i and j.
	Swap(i, j int)
}

Interface is a type, typically a collection, that satisfies shuffle.Interface can be shuffled by the routines in this package.

type Shuffler

type Shuffler rand.Rand

A Shuffler provides Shuffle

func New

func New(src rand.Source) *Shuffler

New returns a new Shuffler that uses random values from src to shuffle

func (*Shuffler) Float64s

func (s *Shuffler) Float64s(a []float64)

Float64s shuffles a slice of float64s.

func (*Shuffler) Ints

func (s *Shuffler) Ints(a []int)

Ints shuffles a slice of ints.

func (*Shuffler) Shuffle

func (s *Shuffler) Shuffle(data Interface)

Shuffle shuffles Data.

As of Go 1.10, it just calls (*rand.Rand).Shuffle(data.Len(), data.Swap).

func (*Shuffler) Strings

func (s *Shuffler) Strings(a []string)

Strings shuffles a slice of strings.

Jump to

Keyboard shortcuts

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