Random library implement by golang
go get -u github.com/enix223/go-random
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)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)Random functions for colorspace
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()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()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()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()