random

package module
v0.0.2-0...-116d00f Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2023 License: MIT Imports: 2 Imported by: 0

README

Random

Go Reference Test TinyGo Go Report Card License Release

Package random is generics based random choice utility. Similar of Python's standard package.

Note: This was created to help me learn about generics.

Requirements

Go 1.18 or later

Usage

See example test code.

Test

go test .

You can use GitHub Actions locally by act.

act -j test

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrEmptySlice = errors.New("slice is empty")

ErrEmptySlice slice is empty.

View Source
var ErrNegativeSize = errors.New("size is negative")

ErrNegativeSize size is negative.

View Source
var ErrSizeTooLarge = errors.New("size is greater than slice")

ErrSizeTooLarge size is greater than slice.

Functions

func Choice

func Choice[T any](in []T) (T, error)

Choice return a random element from slice. Similar of Python's random.choice(). see: https://docs.python.org/3/library/random.html#random.choice

Example
package main

import (
	"fmt"

	"github.com/sg0hsmt/random"
)

func main() {
	// Example for int slice.
	{
		in := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}

		out, err := random.Choice(in)
		if err != nil {
			fmt.Println("choice failed")
			return
		}

		fmt.Println(out) // print one number.
	}

	// Example for string slice.
	{
		in := []string{"a", "b", "c", "d", "e", "f"}

		out, err := random.Choice(in)
		if err != nil {
			fmt.Println("choice failed")
			return
		}

		fmt.Println(out) // print one string.
	}
}
Output:

func Choices

func Choices[T any](in []T, size int) ([]T, error)

Choices returns multiple elements from slice. Similar of Python's random.choices(), But wight is not support. see: https://docs.python.org/3/library/random.html#random.choices

Example
package main

import (
	"fmt"

	"github.com/sg0hsmt/random"
)

func main() {
	// Example for int slice.
	{
		in := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}

		out, err := random.Choices(in, 3)
		if err != nil {
			fmt.Println("choices failed")
			return
		}

		fmt.Println(out) // print three numbers.
	}

	// Example for string slice.
	{
		in := []string{"a", "b", "c", "d", "e", "f"}

		out, err := random.Choices(in, 3)
		if err != nil {
			fmt.Println("choices failed")
			return
		}

		fmt.Println(out) // print three strings.
	}
}
Output:

func Sample

func Sample[T any](in []T, size int) ([]T, error)

Sample returns unique elements from slice. Similar of Python's random.sample(). see: https://docs.python.org/3/library/random.html#random.sample

Example
package main

import (
	"fmt"

	"github.com/sg0hsmt/random"
)

func main() {
	// Example for int slice.
	{
		in := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}

		out, err := random.Sample(in, 3)
		if err != nil {
			fmt.Println("sample failed")
			return
		}

		fmt.Println(out) // print three unique numbers.
	}

	// Example for string slice.
	{
		in := []string{"a", "b", "c", "d", "e", "f"}

		out, err := random.Sample(in, 3)
		if err != nil {
			fmt.Println("sample failed")
			return
		}

		fmt.Println(out) // print three unique strings.
	}
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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