Skip to content

enix223/go-random

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-random

Random library implement by golang

0. Install

go get -u github.com/enix223/go-random

1. Collection

1.1 Sample

Chooses k unique random elements from population with slice/array/string type

  • signature
func Sample(population interface{}, k int) interface{}
  • usage
import (
    "github.com/enix223/go-random/collection"
)

// call with string
// choose 10 items from alphabet letters
// r will be something like this: "AkuOMNBsqe"
r := collection.Sample(random.Alphabet, 10)

// call with slice
// choose 10 items from alphabet letters slice
// r will be something like this: []string{"A", "k", "u", "O", "M", "N", "B", "s", "q", "e"}
r := collection.Sample(random.AlphabetSlice, 10)

// call with array
// choose 10 items from alphabet letters
// r will be something like this: [3]rune{68, 65, 67}
alphabetArray := [10]rune{65, 66, 67, 68}
r := collection.Sample(alphabetArray, 3)

1.2 SampleStringSlice

Chooses k unique random elements from population with string slice type

  • signature
func SampleStringSlice(population []string, k int) []string
  • usage
import (
    "github.com/enix223/go-random/collection"
)

// choose 10 items from alphabet letters slice
// r will be something like this: []string{"A", "k", "u", "O", "M", "N", "B", "s", "q", "e"}
r := collection.SampleStringSlice(random.AlphabetSlice, 10)

2. Color

Random functions for colorspace

2.1 RandomRGB

get a random rgb color array

  • signature
func RandomRGB() [3]uint8
  • example
import (
    "github.com/enix223/go-random/color"
)

// get a random color, eg., []uint8{240, 255, 0}
rgb := color.RandomRGB()

2.2 RandomRGBA

get a random rgb color array

  • signature
func RandomRGBA() [4]uint8
  • example
import (
    "github.com/enix223/go-random/color"
)

// get a random color with alpha channel, eg., []uint8{240, 255, 0, 124}
rgba := color.RandomRGBA()

2.3 RandomRGBString

get a random rgb color in string format "#ffffff"

  • signature
func RandomRGBString() string
  • example
import (
    "github.com/enix223/go-random/color"
)

// get a random color in string format, eg., #ff91ab
rgb := color.RandomRGBString()

2.4 RandomRGBAString

get a random rgb color with alpha channel in string format "#ffffffff"

  • signature
func RandomRGBAString() string
  • example
import (
    "github.com/enix223/go-random/color"
)

// get a random color with alpha channel in string format, eg., #ff91ab19
rgba := color.RandomRGBAString()

About

random library in golang

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages