random

package
v1.99.4 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package random contains a collection of utility functions for generating random numbers and strings.

The random number generator uses the crypto/rand package as the default, but it can be customized by using the WithReader option.

The RandString function, which generates random strings, uses a default character set that includes digits, uppercase and lowercase letters, and symbols. However, it can be customized by using the WithCharByteMap option.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option added in v1.90.0

type Option func(c *Rnd)

Option is the interface that allows to set client options.

func WithByteToCharMap added in v1.90.0

func WithByteToCharMap(cm []byte) Option

WithByteToCharMap overwrites the default slice used to map random bytes to characters. If cm is empty, then the default character map will be used. The maximum cm length is 256, if it is greater than 256, it will be truncated.

type Rnd

type Rnd struct {
	// contains filtered or unexported fields
}

Rnd defines then random number generator.

func New

func New(r io.Reader, opts ...Option) *Rnd

New initialize the random reader. The r argument must be a cryptographically secure random number generator. The crypto/rand.Read is used as default if r == nil.

func (*Rnd) RandString added in v1.90.0

func (r *Rnd) RandString(n int) (string, error)

RandString returns n-characters long random string that can be used as password. It generates n random bytes and maps them to characters using the default character set. The default character set can be overwritten by using the WithCharByteMap option.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/random"
)

func main() {
	r := random.New(nil)

	s, err := r.RandString(16)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(s)
}
Output:

func (*Rnd) RandUint32

func (r *Rnd) RandUint32() uint32

RandUint32 returns a pseudo-random 32-bit value as a uint32 from the default Source. It try to use crypto/rand.Reader, if it fails, it falls back to math/rand/v2.Uint32.

Example
package main

import (
	"fmt"

	"github.com/Vonage/gosrvlib/pkg/random"
)

func main() {
	r := random.New(nil)

	n := r.RandUint32()

	fmt.Println(n)
}
Output:

func (*Rnd) RandUint64

func (r *Rnd) RandUint64() uint64

RandUint64 returns a pseudo-random 64-bit value as a uint64 from the default Source. It try to use crypto/rand.Reader, if it fails, it falls back to math/rand/v2.Uint64.

Example
package main

import (
	"fmt"

	"github.com/Vonage/gosrvlib/pkg/random"
)

func main() {
	r := random.New(nil)

	n := r.RandUint64()

	fmt.Println(n)
}
Output:

func (*Rnd) RandomBytes

func (r *Rnd) RandomBytes(n int) ([]byte, error)

RandomBytes generates a slice of random bytes with the specified length.

Jump to

Keyboard shortcuts

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