randutil

package module
v0.0.0-...-2bb1b66 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2015 License: GPL-3.0 Imports: 3 Imported by: 69

README

randutil - Random number/string utilities for the Go language

This package contains assorted utility functions for creating random numbers and strings. I use this package quite a bit, especially when testing other projects. However I make no claims about the cryptographic security or other properties of the random(-ish?) numbers and strings it produces. Use at your own risk.

API Documentation

See GoDoc for automatically generated documentation.

Status

Build Status Travis Build Status

Documentation

Overview

Package randutil provides various convenience functions for dealing with random numbers and strings.

Index

Constants

View Source
const (
	// Set of characters to use for generating random strings
	Alphabet     = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	Numerals     = "1234567890"
	Alphanumeric = Alphabet + Numerals
	Ascii        = Alphanumeric + "~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:`"
)

Variables

View Source
var MinMaxError = errors.New("Min cannot be greater than max.")

Functions

func AlphaString

func AlphaString(n int) (string, error)

AlphaString returns a random alphanumeric string n characters long.

func AlphaStringRange

func AlphaStringRange(min, max int) (string, error)

AlphaRange returns a random alphanumeric string at least min and no more than max characters long.

func ChoiceInt

func ChoiceInt(choices []int) (int, error)

ChoiceInt returns a random selection from an array of integers.

func ChoiceString

func ChoiceString(choices []string) (string, error)

ChoiceString returns a random selection from an array of strings.

func IntRange

func IntRange(min, max int) (int, error)

IntRange returns a random integer in the range from min to max.

func String

func String(n int, charset string) (string, error)

String returns a random string n characters long, composed of entities from charset.

func StringRange

func StringRange(min, max int, charset string) (string, error)

StringRange returns a random string at least min and no more than max characters long, composed of entitites from charset.

Types

type Choice

type Choice struct {
	Weight int
	Item   interface{}
}

A Choice contains a generic item and a weight controlling the frequency with which it will be selected.

func WeightedChoice

func WeightedChoice(choices []Choice) (Choice, error)

WeightedChoice used weighted random selection to return one of the supplied choices. Weights of 0 are never selected. All other weight values are relative. E.g. if you have two choices both weighted 3, they will be returned equally often; and each will be returned 3 times as often as a choice weighted 1.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL