gorando

gorando is a Golang module I wrote to generate random strings.

Example

This script will create an 12-character string containing zero or more of “a”, “b” and “c”:

package main

import (
    "fmt"
    "github.com/cariad/gorando"
)

func main() {
    s, _ := gorando.GetString("abc", 12)
    fmt.Println(s)
}

Run in the Go Playground

When I ran that example, I got this output:

baabcbccabca

"abc" is the pool of runes which should be considered for inclusion. In the output above, each string contains only runes from that string.

A rune isn’t guaranteed to be included, but the string definitely won’t contain a rune that’s not in the pool.

The pool can be any Unicode string. Here’s an example with Japanese characters and emoji:

package main

import (
    "fmt"
    "github.com/cariad/gorando"
)

func main() {
    j, _ := gorando.GetString("こんにちは", 12)
    fmt.Println(j)

    e, _ := gorando.GetString("👻😺👽", 12)
    fmt.Println(e)
}

Run in the Go Playground

And again, the output contains only the runes that were described by the pool:

んこにちこちんんこんはこ
👻👻👽👽👽👽👽👻😺👻😺👻

Usage

Import "github.com/cariad/gorando" and use the following functions:

func GetRunes(pool string, count int) ([]rune, error)

GetRunes returns a slice of runes of length count populated by a random pick of runes out of the pool string.

func GetString(pool string, length int) (string, error)

GetString returns a string of length length populated by a random pick of runes out of the pool string.

Security

This package uses crypto/rand to generate random numbers, to avoid the predictability of math/random.

Pools and random picks are intentionally not logged.

gorando is used by:

Report a bug, request a feature or ask a question

Please use the issues page to raise a bug, request a feature or ask a question.

Licence

gorando is published under the MIT License.

Comments