Random Integers and Strings

The package mrand is a wrapper for Go’s math/rand package.  It provides three functions: Int, Int64, and String. Both Int and Int64 are variadic functions so they may be passed minimum and maximum values or may be used without arguments in which case they will return a random number between zero and their respective maximum values. The String function requires one length argument and will return an alpha-numeric string.

Example:

package main

import (
	"fmt"
	"github.com/mkmueller/mrand"
)

func main () {
	fmt.Printf("Integer between 1 and 100: %v\n", mrand.Int(1,100))
	fmt.Printf("Integer between 0 and MaxInt32: %v\n", mrand.Int())
	fmt.Printf("64bit Integer between 0 and MaxInt64: %v\n", mrand.Int64())
	fmt.Printf("Random alpha-numeric string: %v\n", mrand.String(24))
}

View source

Leave a Reply

Your email address will not be published. Required fields are marked *