Skip to content
generated from atomicgo/template

🎲 Minimalistic random generator for numbers, strings, booleans, etc.

License

Notifications You must be signed in to change notification settings

atomicgo/random

Repository files navigation

AtomicGo | random

Downloads Latest Release Tests Coverage Unit test count License: MIT Go report


Documentation | Contributing | Code of Conduct


AtomicGo

go get atomicgo.dev/random

random

import "atomicgo.dev/random"

Package random provides random generators for most inbuilt types (int, float, string, bool, etc...).

All functions are easily accessible and don't require any setup.

Index

Constants

StringSet constants are sets of characters that can be used in random strings.

const (
    StringSetLowercase = "abcdefghijklmnopqrstuvwxyz"
    StringSetUppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    StringSetNumbers   = "0123456789"
    StringSetSymbols   = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
    StringSetAll       = StringSetLowercase + StringSetUppercase + StringSetNumbers + StringSetSymbols
)

func Bool

func Bool() bool

Bool returns a random boolean.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	if random.Bool() {
		fmt.Println("Heads")
	} else {
		fmt.Println("Tails")
	}

}

Output

Heads

func BoolSlice(count int) []bool

BoolSlice returns a slice of random booleans. If count is less than 0, BoolSlice will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.BoolSlice(5)
	fmt.Println(s)

}

Output

[true true false false true]

func Entries

func Entries[T any](s []T, n int) []T

Entries returns a slice of n random elements from a slice. If n is less than 0, Entries will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := []int{1, 2, 3, 4, 5}
	fmt.Print(random.Entries(s, 3))

}

Output

[4 4 5]

func EntriesUnique[T any](s []T, n int) []T

EntriesUnique returns a slice of n unique random elements from a slice. If n is less than 0, EntriesUnique will panic. If n is greater than the length of s, EntriesUnique will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := []int{1, 2, 3, 4, 5}
	fmt.Print(random.EntriesUnique(s, 3))

}

Output

[4 5 2]

func Entry

func Entry[T any](s []T) T

Entry returns a random element from a slice.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := []int{1, 2, 3, 4, 5}
	fmt.Print(random.Entry(s))

}

Output

4

func Float32

func Float32(min, max float32) float32

Float32 returns a random float32 between min and max, inclusive. If min > max, Float32 panics.

package main

import (
	"fmt"
	"math"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	var min, max float32 = 0, math.MaxFloat32
	res := random.Float32(min, max)

	fmt.Println(res)
}

Output

0.6287385

func Float32Slice(min, max float32, count int) []float32

Float32Slice returns a slice of random float32s between min and max, inclusive. If min > max, Float32Slice will panic. If count is less or euqal to 0, Float32SliceUnique will return an empty slice.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Float32Slice(1, 100, 10)

	fmt.Println(s)
}

Output

[63.245113 34.879265 42.326313 96.07255 88.959915 66.83283 57.638523 84.8611 92.85987 97.56791]

func Float32SliceUnique(min, max float32, count int) []float32

Float32SliceUnique returns a slice of unique random float32s between min and max, inclusive. If min > max, Float32SliceUnique will panic. If count is equal or less than 0, Float32SliceUnique will return an empty slice. If count is greater than the number of float32s between min and max, Float32SliceUnique will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Float32SliceUnique(1, 100, 10)

	fmt.Println(s)
}

Output

[63.245113 34.879265 42.326313 96.07255 88.959915 66.83283 57.638523 84.8611 92.85987 97.56791]

func Float64

func Float64(min, max float64) float64

Float64 returns a random float64 between min and max, inclusive. If min > max, Float64 panics.

package main

import (
	"fmt"
	"math"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	var min, max float64 = 0, math.MaxFloat64
	res := random.Float64(min, max)

	fmt.Println(res)
}

Output

0.6287385421322026

func Float64Slice(min, max float64, count int) []float64

Float64Slice returns a slice of random float64s between min and max, inclusive. If min > max, Float64Slice will panic. If count is less or euqal to 0, Float64SliceUnique will return an empty slice.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Float64Slice(1, 100, 10)

	fmt.Println(s)
}

Output

[63.24511567108806 34.87926481692567 42.326312224687584 96.0725445866481 88.95991853003598 66.83282968657517 57.63852232585161 84.86109852327596 92.85987365256264 97.56790999047796]

func Float64SliceUnique(min, max float64, count int) []float64

Float64SliceUnique returns a slice of unique random float64s between min and max, inclusive. If min > max, Float64SliceUnique will panic. If count is equal or less than 0, Float64SliceUnique will return an empty slice. If count is greater than the number of float64s between min and max, Float64SliceUnique will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Float64SliceUnique(1, 100, 10)

	fmt.Println(s)
}

Output

[63.24511567108806 34.87926481692567 42.326312224687584 96.0725445866481 88.95991853003598 66.83282968657517 57.63852232585161 84.86109852327596 92.85987365256264 97.56790999047796]

func Int

func Int(min, max int) int

Int returns a random integer between min and max, inclusive. If min > max, Int will panic.

package main

import (
	"fmt"
	"math"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	min, max := 0, math.MaxInt
	res := random.Int(min, max)

	fmt.Println(res)
}

Output

5799089487994996006

func Int16

func Int16(min, max int16) int16

Int16 returns a random int16 between min and max, inclusive. If min > max, Int16 panics.

package main

import (
	"fmt"
	"math"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	var min, max int16 = 0, math.MaxInt16
	res := random.Int16(min, max)

	fmt.Println(res)
}

Output

298

func Int16Slice(min, max, count int16) []int16

Int16Slice returns a slice of random int16s between min and max, inclusive. If min > max, Int16Slice will panic. If count is less or euqal to 0, Int16SliceUnique will return an empty slice.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Int16Slice(1, 100, 10)

	fmt.Println(s)
}

Output

[39 79 100 52 17 49 95 22 84 50]

func Int16SliceUnique(min, max int16, count int) []int16

Int16SliceUnique returns a slice of unique random int16s between min and max, inclusive. If min > max, Int16SliceUnique will panic. If count is equal or less than 0, Int16SliceUnique will return an empty slice. If count is greater than the number of int16s between min and max, Int16SliceUnique will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Int16SliceUnique(1, 100, 10)

	fmt.Println(s)
}

Output

[39 79 100 52 17 49 95 22 84 50]

func Int32

func Int32(min, max int32) int32

Int32 returns a random int32 between min and max, inclusive. If min > max, Int32 panics.

package main

import (
	"fmt"
	"math"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	var min, max int32 = 0, math.MaxInt32
	res := random.Int32(min, max)

	fmt.Println(res)
}

Output

1350205738

func Int32Slice(min, max, count int32) []int32

Int32Slice returns a slice of random int32s between min and max, inclusive. If min > max, Int32Slice will panic. If count is less or euqal to 0, Int32SliceUnique will return an empty slice.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Int32Slice(1, 100, 10)

	fmt.Println(s)
}

Output

[39 79 100 52 17 49 95 22 84 50]

func Int32SliceUnique(min, max int32, count int) []int32

Int32SliceUnique returns a slice of unique random int32s between min and max, inclusive. If min > max, Int32SliceUnique will panic. If count is equal or less than 0, Int32SliceUnique will return an empty slice. If count is greater than the number of int32s between min and max, Int32SliceUnique will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Int32SliceUnique(1, 100, 10)

	fmt.Println(s)
}

Output

[39 79 100 52 17 49 95 22 84 50]

func Int64

func Int64(min, max int64) int64

Int64 returns a random int64 between min and max, inclusive. If min > max, Int64 panics.

package main

import (
	"fmt"
	"math"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	var min, max int64 = 0, math.MaxInt64
	res := random.Int64(min, max)

	fmt.Println(res)
}

Output

5799089487994996006

func Int64Slice(min, max, count int64) []int64

Int64Slice returns a slice of random int64s between min and max, inclusive. If min > max, Int64Slice will panic. If count is less or euqal to 0, Int64SliceUnique will return an empty slice.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Int64Slice(1, 100, 10)

	fmt.Println(s)
}

Output

[7 7 53 64 9 58 71 56 45 34]

func Int64SliceUnique(min, max int64, count int) []int64

Int64SliceUnique returns a slice of unique random int64s between min and max, inclusive. If min > max, Int64SliceUnique will panic. If count is equal or less than 0, Int64SliceUnique will return an empty slice. If count is greater than the number of int64s between min and max, Int64SliceUnique will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Int64SliceUnique(1, 100, 10)

	fmt.Println(s)
}

Output

[7 53 64 9 58 71 56 45 34 57]

func Int8

func Int8(min, max int8) int8

Int8 returns a random int8 between min and max, inclusive. If min > max, Int8 panics.

package main

import (
	"fmt"
	"math"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	var min, max int8 = 0, math.MaxInt8
	res := random.Int8(min, max)

	fmt.Println(res)
}

Output

42

func Int8Slice(min, max, count int8) []int8

Int8Slice returns a slice of random int8s between min and max, inclusive. If min > max, Int8Slice will panic. If count is less or euqal to 0, Int8SliceUnique will return an empty slice.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Int8Slice(1, 100, 10)

	fmt.Println(s)
}

Output

[39 79 100 52 17 49 95 22 84 50]

func Int8SliceUnique(min, max int8, count int) []int8

Int8SliceUnique returns a slice of unique random int8s between min and max, inclusive. If min > max, Int8SliceUnique will panic. If count is equal or less than 0, Int8SliceUnique will return an empty slice. If count is greater than the number of int8s between min and max, Int8SliceUnique will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Int8SliceUnique(1, 100, 10)

	fmt.Println(s)
}

Output

[39 79 100 52 17 49 95 22 84 50]

func IntSlice(min, max, count int) []int

IntSlice returns a slice of random integers between min and max, inclusive. If min > max, IntSlice will panic. If count is less or euqal to 0, IntSliceUnique will return an empty slice.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.IntSlice(1, 100, 10)

	fmt.Println(s)
}

Output

[39 79 100 52 17 49 95 22 84 50]

func IntSliceUnique(min, max, count int) []int

IntSliceUnique returns a slice of unique random integers between min and max, inclusive. If min > max, IntSliceUnique will panic. If count is equal or less than 0, IntSliceUnique will return an empty slice. If count is greater than the number of integers between min and max, IntSliceUnique will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.IntSliceUnique(1, 100, 10)

	fmt.Println(s)
}

Output

[39 79 100 52 17 49 95 22 84 50]

func Probability(p float64) bool

Probability returns true with a probability of p. 1 = 100%; 0.5 = 50%; 0 = 0% If p is less than 0 or greater than 1, Probability panics.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	// We are using a pronged coin with a 80% chance of landing on Heads.
	if random.Probability(0.8) {
		fmt.Println("Heads")
	} else {
		fmt.Println("Tails")
	}

}

Output

Heads

func Seed

func Seed(seed int64)

Seed sets the seed of the random generator. By default, the seed is already randomized on package initialization. You only need to call this function if you need a specific seed.

package main

import (
	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337)
}

func Shuffle

func Shuffle[T any](s []T) []T

Shuffle returns a shuffled copy of a slice.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := []int{1, 2, 3, 4, 5}
	fmt.Print(random.Shuffle(s))

}

Output

[1 3 5 2 4]

func String

func String(l int, set string) string

String returns a random string of length l, using the characters in the set. If l is less than 0, String panics. If the set is empty, or l is 0, String returns an empty string.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	fmt.Print(random.String(5, random.StringSetLowercase+random.StringSetUppercase))

}

Output

YsFVm

func StringSlice(l int, set string, count int) []string

StringSlice returns a slice of random strings of length l, using the characters in the set. If l is less than 0, StringSlice panics. If the set is empty, or l is 0, StringSlice returns an empty slice. If count is less than 0, StringSlice will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.StringSlice(5, random.StringSetLowercase+random.StringSetUppercase, 5)
	fmt.Println(s)

}

Output

[YsFVm KAfdn AIdJS kxApO apmsT]

func Uint

func Uint(min, max uint) uint

Uint returns a random uint between min and max, inclusive. If min > max, Uint will panic.

package main

import (
	"fmt"
	"math"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	var min, max uint = 0, math.MaxUint
	res := random.Uint(min, max)

	fmt.Println(res)
}

Output

15022461524849771814

func Uint16

func Uint16(min, max uint16) uint16

Uint16 returns a random uint16 between min and max, inclusive. If min > max, Uint16 panics.

package main

import (
	"fmt"
	"math"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	var min, max uint16 = 0, math.MaxUint16
	res := random.Uint16(min, max)

	fmt.Println(res)
}

Output

596

func Uint16Slice(min, max, count uint16) []uint16

Uint16Slice returns a slice of random uint16s between min and max, inclusive. If min > max, Uint16Slice will panic. If count is less or euqal to 0, Uint16SliceUnique will return an empty slice.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Uint16Slice(1, 100, 10)

	fmt.Println(s)
}

Output

[77 58 99 3 33 97 89 43 68 100]

func Uint16SliceUnique(min, max uint16, count int) []uint16

Uint16SliceUnique returns a slice of unique random uint16s between min and max, inclusive. If min > max, Uint16SliceUnique will panic. If count is equal or less than 0, Uint16SliceUnique will return an empty slice. If count is greater than the number of uint16s between min and max, Uint16SliceUnique will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Uint16SliceUnique(1, 100, 10)

	fmt.Println(s)
}

Output

[77 58 99 3 33 97 89 43 68 100]

func Uint32

func Uint32(min, max uint32) uint32

Uint32 returns a random uint32 between min and max, inclusive. If min > max, Uint32 panics.

package main

import (
	"fmt"
	"math"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	var min, max uint32 = 0, math.MaxUint32
	res := random.Uint32(min, max)

	fmt.Println(res)
}

Output

2700411476

func Uint32Slice(min, max, count uint32) []uint32

Uint32Slice returns a slice of random uint32s between min and max, inclusive. If min > max, Uint32Slice will panic. If count is less or euqal to 0, Uint32SliceUnique will return an empty slice.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Uint32Slice(1, 100, 10)

	fmt.Println(s)
}

Output

[77 58 99 3 33 97 89 43 68 100]

func Uint32SliceUnique(min, max uint32, count int) []uint32

Uint32SliceUnique returns a slice of unique random uint32s between min and max, inclusive. If min > max, Uint32SliceUnique will panic. If count is equal or less than 0, Uint32SliceUnique will return an empty slice. If count is greater than the number of uint32s between min and max, Uint32SliceUnique will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Uint32SliceUnique(1, 100, 10)

	fmt.Println(s)
}

Output

[77 58 99 3 33 97 89 43 68 100]

func Uint64

func Uint64(min, max uint64) uint64

Uint64 returns a random uint64 between min and max, inclusive. If min > max, Uint64 panics.

package main

import (
	"fmt"
	"math"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	var min, max uint64 = 0, math.MaxUint64
	res := random.Uint64(min, max)

	fmt.Println(res)
}

Output

15022461524849771814

func Uint64Slice(min, max, count uint64) []uint64

Uint64Slice returns a slice of random uint64s between min and max, inclusive. If min > max, Uint64Slice will panic. If count is less or euqal to 0, Uint64SliceUnique will return an empty slice.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Uint64Slice(1, 100, 10)

	fmt.Println(s)
}

Output

[15 15 53 72 9 66 79 64 53 34]

func Uint64SliceUnique(min, max uint64, count int) []uint64

Uint64SliceUnique returns a slice of unique random uint64s between min and max, inclusive. If min > max, Uint64SliceUnique will panic. If count is equal or less than 0, Uint64SliceUnique will return an empty slice. If count is greater than the number of uint64s between min and max, Uint64SliceUnique will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Uint64SliceUnique(1, 100, 10)

	fmt.Println(s)
}

Output

[15 53 72 9 66 79 64 34 57 17]

func Uint8

func Uint8(min, max uint8) uint8

Uint8 returns a random uint8 between min and max, inclusive. If min > max, Uint8 panics.

package main

import (
	"fmt"
	"math"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	var min, max uint8 = 0, math.MaxUint8
	res := random.Uint8(min, max)

	fmt.Println(res)
}

Output

84

func Uint8Slice(min, max, count uint8) []uint8

Uint8Slice returns a slice of random uint8s between min and max, inclusive. If min > max, Uint8Slice will panic. If count is less or euqal to 0, Uint8SliceUnique will return an empty slice.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Uint8Slice(1, 100, 10)

	fmt.Println(s)
}

Output

[77 58 99 3 33 97 89 43 68 100]

func Uint8SliceUnique(min, max uint8, count int) []uint8

Uint8SliceUnique returns a slice of unique random uint8s between min and max, inclusive. If min > max, Uint8SliceUnique will panic. If count is equal or less than 0, Uint8SliceUnique will return an empty slice. If count is greater than the number of uint8s between min and max, Uint8SliceUnique will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.Uint8SliceUnique(1, 100, 10)

	fmt.Println(s)
}

Output

[77 58 99 3 33 97 89 43 68 100]

func UintSlice(min, max, count uint) []uint

UintSlice returns a slice of random uints between min and max, inclusive. If min > max, UintSlice will panic. If count is less or euqal to 0, UintSliceUnique will return an empty slice.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.UintSlice(1, 100, 10)

	fmt.Println(s)
}

Output

[15 15 53 72 9 66 79 64 53 34]

func UintSliceUnique(min, max uint, count int) []uint

UintSliceUnique returns a slice of unique random uints between min and max, inclusive. If min > max, UintSliceUnique will panic. If count is equal or less than 0, UintSliceUnique will return an empty slice. If count is greater than the number of uints between min and max, UintSliceUnique will panic.

package main

import (
	"fmt"

	"atomicgo.dev/random"
)

func main() {
	random.Seed(1337) // Set seed for deterministic output, not required

	s := random.UintSliceUnique(1, 100, 10)

	fmt.Println(s)
}

Output

[15 53 72 9 66 79 64 34 57 17]

Generated by gomarkdoc


AtomicGo.dev  ·  with ❀️ by @MarvinJWendt | MarvinJWendt.com