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 ¶
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 ¶
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 ¶
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.
Click to show internal directories.
Click to hide internal directories.