randomizer

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2021 License: MIT Imports: 5 Imported by: 1

README

go-randomizer

GoDoc Release Go Report Card Coverage

Randomizer is a library that randomizes different types of words, such as Adjectives and Nouns, or just Words and handles all of Go's randomizing concurrency for you. Also can pick a time between a start and end date.

Installation

go get github.com/Squwid/go-randomizer

How to Use

The math/rand seed is set as time.Now().UnixNano() when the package is initialized. A custom seed can also be used by calling randomizer.NewRandSourceFromSource(source int64).

package main

import (
    "fmt"

    "github.com/Squwid/go-randomizer"
)

func main() {
    // Print a snake case Adjective - Noun 
    // ex. dizzy_horse
    snakeCase := fmt.Sprintf("%s_%s", randomizer.Adjective(), randomizer.Noun())
    fmt.Println(snakeCase)

    // Print a title case Adjective - Noun
    // ex. Pretty Eagle
    titleCase := strings.Title(fmt.Sprintf("%s %s", randomizer.Adjective(), randomizer.Noun()))
    fmt.Println(titleCase)

    // Print a fake title between 5 and 10 words
    // ex. Astonishing Tiger Massive Glorious Dress Walrus Swanky
    title := strings.Title(strings.Join(randomizer.Words(randomizer.Number(5, 10)), " "))
    fmt.Println(title)

    // Print a random time in 2020
    // ex. 2020-06-22 13:34:06 -0400 EDT
    begin := time.Date(2020, 1, 0, 0, 0, 0, 0, time.UTC)
	end := time.Date(2021, 1, 0, 0, 0, 0, 0, time.UTC)
    date := randomizer.Date(begin, end)
    fmt.Println(date.String())
}

Set Custom Rand Seed

Setting a custom seed is effective for testing, since having the same seed will guarantee that randomize functions will have the same randomized order.

package main

import (
    "github.com/Squwid/go-randomizer"
)

func main() {
    randomizer.NewRandSourceFromSource(0) // Extremely effective for testing, randomize will always have same order
}

Documentation

Overview

package randomizer implements an extremely easy way to randomly generate words, numbers, or time without having to worry about random seeds

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Adjective

func Adjective() string

Adjective returns a random adjective

func Date

func Date(begin, end time.Time) time.Time

func NewRandSource

func NewRandSource()

NewRandSource creates a new random source using the current time as the seed

func NewRandSourceFromSource

func NewRandSourceFromSource(source int64)

NewRandSourceFromSource uses a user provided source to set the random source

func Noun

func Noun() string

Noun returns a random noun

func Number

func Number(min, max int) int

Number returns a random number between and inclusive of min and max

func Word

func Word() string

Word returns a random word, either an adjective or a noun

func Words

func Words(n int) []string

Words returns a slice of words of count n

Types

This section is empty.

Jump to

Keyboard shortcuts

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