randomdata

package module
v0.0.0-...-7c88b45 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2024 License: MIT Imports: 16 Imported by: 0

README

go-randomdata

randomdata is a tiny help suite for generating random data such as

  • first names (male or female)
  • last names
  • full names (male or female)
  • country names (full name or iso 3166.1 alpha-2 or alpha-3)
  • locales / language tags (bcp-47)
  • random email address
  • city names
  • American state names (two chars or full)
  • random numbers (in an interval)
  • random paragraphs
  • random bool values
  • postal- or zip-codes formatted for a range of different countries.
  • american sounding addresses / street names
  • silly names - suitable for names of things
  • random days
  • random months
  • random full date
  • random full profile
  • random date inside range
  • random phone number

Credit where credit is due

This repository is a fork of the go-randomdata created by David Pallinder.

This fork was created to change fundamentally how the random source is used. In the original work, the random source is defined globally. In this version, you need to create a random generator explicitly.

Installation

go get github.com/grandper/go-randomdata

Usage

package main

import (
    "fmt"
    "github.com/Pallinder/go-randomdata"
)

func main() {
    r := randomdata.FromSeed(1234)

	// Print an int from the half-open interval [0, 10)
	fmt.Println(r.Intn(10))

	// Print a float64 from the half-open interval [0.0, 1.0)
	fmt.Println(r.Float64())

	// Print a random bool
	fmt.Println(r.Boolean())

	// Print a duration
	fmt.Println(r.Duration(24 * time.Hour).Hours())

	// Print a time between now and Tomorrow
	fmt.Println(r.Time(time.Now(), 24*time.Hour))

	// Print a time range now and Tomorrow
	fmt.Println(r.TimeRange(time.Now(), 24*time.Hour))

    // Print a random silly name
    fmt.Println(r.SillyName())

    // Print a male title
    fmt.Println(r.Title(randomdata.Male))

    // Print a female title
    fmt.Println(r.Title(randomdata.Female))

    // Print a title with random gender
    fmt.Println(r.Title(randomdata.RandomGender))

    // Print a male first name
    fmt.Println(r.FirstName(randomdata.Male))

    // Print a female first name
    fmt.Println(r.FirstName(randomdata.Female))

    // Print a last name
    fmt.Println(r.LastName())

    // Print a male name
    fmt.Println(r.FullName(randomdata.Male))

    // Print a female name
    fmt.Println(r.FullName(randomdata.Female))

    // Print a name with random gender
    fmt.Println(r.FullName(randomdata.RandomGender))

    // Print an email
    fmt.Println(r.Email())

    // Print a country with full text representation
    fmt.Println(r.Country(randomdata.FullCountry))

    // Print a country using ISO 3166-1 alpha-2
    fmt.Println(r.Country(randomdata.TwoCharCountry))

    // Print a country using ISO 3166-1 alpha-3
    fmt.Println(r.Country(randomdata.ThreeCharCountry))
    
    // Print BCP 47 language tag
    fmt.Println(r.Locale())

    // Print a currency using ISO 4217
    fmt.Println(r.Currency())

    // Print the name of a random city
    fmt.Println(r.City())

    // Print the name of a random american state
    fmt.Println(r.State(randomdata.Large))

    // Print the name of a random american state using two chars
    fmt.Println(r.State(randomdata.Small))

    // Print an american sounding street name
    fmt.Println(r.Street())

    // Print an american sounding address
    fmt.Println(r.Address())

    // Print a random number >= 10 and < 20
    fmt.Println(r.Number(10, 20))

    // Print a number >= 0 and < 20
    fmt.Println(r.Number(20))

    // Print a random float >= 0 and < 20 with decimal point 3
    fmt.Println(r.Decimal(0, 20, 3))

    // Print a random float >= 10 and < 20
    fmt.Println(r.Decimal(10, 20))

    // Print a random float >= 0 and < 20
    fmt.Println(r.Decimal(20))

    // Print a paragraph
    fmt.Println(r.Paragraph())

    // Print a postal code
    fmt.Println(r.PostalCode("SE"))

    // Print a set of 2 random numbers as a string
    fmt.Println(r.StringNumber(2, "-"))

    // Print a set of 2 random 3-Digits numbers as a string
    fmt.Println(r.StringNumberExt(2, "-", 3))

    // Print a random string sampled from a list of strings
    fmt.Println(r.StringFrom("my string 1", "my string 2", "my string 3"))

    // Print a valid random IPv4 address
    fmt.Println(r.IpV4Address())

    // Print a valid random IPv6 address
    fmt.Println(r.IpV6Address())

    // Print a browser's user agent string
    fmt.Println(r.UserAgentString())

    // Print a day
    fmt.Println(r.Day())

    // Print a month
    fmt.Println(r.Month())

    // Print full date like Monday 22 Aug 2016
    fmt.Println(r.FullDate())

    // Print full date <= Monday 22 Aug 2016
    fmt.Println(r.FullDateInRange("2016-08-22"))

    // Print full date >= Monday 01 Aug 2016 and <= Monday 22 Aug 2016
    fmt.Println(r.FullDateInRange("2016-08-01", "2016-08-22"))

    // Print phone number according to e.164
    fmt.Println(r.PhoneNumber())

    // Get a complete and randomised profile of data generally used for users
    // There are many fields in the profile to use check the Profile struct definition in fullprofile.go
    profile := randomdata.GenerateProfile(randomdata.Male | randomdata.Female | randomdata.RandomGender)
    fmt.Printf("The new profile's username is: %s and password (md5): %s\n", profile.Login.Username, profile.Login.Md5)

    // Get a random country-localised street name for Great Britain
    fmt.Println(r.StreetForCountry("GB"))
    // Get a random country-localised street name for USA
    fmt.Println(r.StreetForCountry("US"))

    // Get a random country-localised province for Great Britain
    fmt.Println(r.ProvinceForCountry("GB"))
    // Get a random country-localised province for USA
    fmt.Println(r.ProvinceForCountry("US"))
}

Documentation

Overview

Package randomdata implements a bunch of simple ways to generate (pseudo) random data

Example
r := FromSeed(1234)

// Print an int from the half-open interval [0, 10)
fmt.Println(r.Intn(10))

// Print a float64 from the half-open interval [0.0, 1.0)
fmt.Println(r.Float64())

// Print a random bool
fmt.Println(r.Boolean())

// Print a duration
fmt.Println(r.Duration(24 * time.Hour).Hours())

// Print a time between now and Tomorrow
fmt.Println(r.Time(time.Now(), 24*time.Hour))

// Print a time range now and Tomorrow
fmt.Println(r.TimeRange(time.Now(), 24*time.Hour))

// Print a male title
fmt.Println(r.FirstName(Male))

// Print a female title
fmt.Println(r.FirstName(Female))

// Print a title with random gender
fmt.Println(r.FirstName(RandomGender))

// Print a male first name
fmt.Println(r.FirstName(Male))

// Print a female first name
fmt.Println(r.FirstName(Female))

// Print a first name with random gender
fmt.Println(r.FirstName(RandomGender))

// Print a last name
fmt.Println(r.LastName())

// Print a male name
fmt.Println(r.FullName(Male))

// Print a female name
fmt.Println(r.FullName(Female))

// Print a name with random gender
fmt.Println(r.FullName(RandomGender))

// Print a random email
fmt.Println(r.Email())

// Print a country with full text representation
fmt.Println(r.Country(FullCountry))

// Print a country using ISO 3166-1 alpha-3
fmt.Println(r.Country(ThreeCharCountry))

// Print a country using ISO 3166-1 alpha-2
fmt.Println(r.Country(TwoCharCountry))

// Print a currency using ISO 4217
fmt.Println(r.Currency())

// Print the name of a random city
fmt.Println(r.City())

// Print the name of a random american state
fmt.Println(r.State(Large))

// Print the name of a random american state using two letters
fmt.Println(r.State(Small))

// Print a random number >= 10 and <= 20
fmt.Println(r.Number(10, 20))

// Print a number >= 0 and <= 20
fmt.Println(r.Number(20))

// Print a random float >= 0 and <= 20 with decimal point 3
fmt.Println(r.Decimal(0, 20, 3))

// Print a random float >= 10 and <= 20
fmt.Println(r.Decimal(10, 20))

// Print a random float >= 0 and <= 20
fmt.Println(r.Decimal(20))

// Print a paragraph
fmt.Println(r.Paragraph())

// Print a random postalcode from Sweden
fmt.Println(r.PostalCode("SE"))

// Print a random american sounding street name
fmt.Println(r.Street())

// Print a random american address
fmt.Println(r.Address())

// Print a random string of numbers
fmt.Println(r.StringNumber(2, "-"))

// Print a set of 2 random 3-Digits numbers as a string
fmt.Println(r.StringNumberExt(2, "-", 3))

// Print a random IPv4 address
fmt.Println(r.IpV4Address())

// Print a random IPv6 address
fmt.Println(r.IpV6Address())

// Print a random day
fmt.Println(r.Day())

// Print a month
fmt.Println(r.Month())

// Print full date
fmt.Println(r.FullDate())

Index

Examples

Constants

View Source
const (
	Male         int = 0
	Female       int = 1
	RandomGender int = 2
)
View Source
const (
	Small int = 0
	Large int = 1
)
View Source
const (
	FullCountry      = 0
	TwoCharCountry   = 1
	ThreeCharCountry = 2
)
View Source
const (
	DateInputLayout  = "2006-01-02"
	DateOutputLayout = "Monday 2 Jan 2006"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Profile

type Profile struct {
	Gender string `json:"gender"`
	Name   struct {
		First string `json:"first"`
		Last  string `json:"last"`
		Title string `json:"title"`
	} `json:"name"`
	Location struct {
		Street   string `json:"street"`
		City     string `json:"city"`
		State    string `json:"state"`
		Postcode int    `json:"postcode"`
	} `json:"location"`

	Email string `json:"email"`
	Login struct {
		Username string `json:"username"`
		Password string `json:"password"`
		Salt     string `json:"salt"`
		Md5      string `json:"md5"`
		Sha1     string `json:"sha1"`
		Sha256   string `json:"sha256"`
	} `json:"login"`

	Dob        string `json:"dob"`
	Registered string `json:"registered"`
	Phone      string `json:"phone"`
	Cell       string `json:"cell"`

	ID struct {
		Name  string      `json:"name"`
		Value interface{} `json:"value"`
	} `json:"id"`

	Picture struct {
		Large     string `json:"large"`
		Medium    string `json:"medium"`
		Thumbnail string `json:"thumbnail"`
	} `json:"picture"`
	Nat string `json:"nat"`
}

Profile contains all the data related to a complete profil.

type Rand

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

Rand is a source of random numbers.

func FromRand

func FromRand(randToUse *rand.Rand) *Rand

FromRand creates a new source of random numbers from a rand.Rand.

func FromSeed

func FromSeed(seed int64) *Rand

FromSeed creates a new source of random numbers using a seed.

func (*Rand) Address

func (r *Rand) Address() string

Address returns an american style address.

func (*Rand) Adjective

func (r *Rand) Adjective() string

Adjective returns a random adjective.

func (*Rand) Alphanumeric

func (r *Rand) Alphanumeric(length int) string

Alphanumeric returns a random alphanumeric string consits of [0-9a-zA-Z].

func (*Rand) Boolean

func (r *Rand) Boolean() bool

Boolean returns randomly either true or false.

func (*Rand) BoundedDigits

func (r *Rand) BoundedDigits(digits, low, high int) string

BoundedDigits generates a string of N random digits, padded with zeros if necessary. The output is restricted to the given range.

func (*Rand) City

func (r *Rand) City() string

City returns a random city.

func (*Rand) Country

func (r *Rand) Country(countryStyle int64) string

Country returns a random country, countryStyle decides what kind of format the returned country will have.

func (*Rand) Currency

func (r *Rand) Currency() string

Currency returns a random currency under ISO 4217 format.

func (*Rand) Day

func (r *Rand) Day() string

Day returns random day.

func (*Rand) Decimal

func (r *Rand) Decimal(numberRange ...int) float64

func (*Rand) Digits

func (r *Rand) Digits(digits int) string

Digits generates a string of N random digits, padded with zeros if necessary.

func (*Rand) Duration

func (r *Rand) Duration(maxDuration time.Duration) time.Duration

Duration returns a random duration between 0 and the specified max duration.

func (*Rand) Email

func (r *Rand) Email() string

Email returns a random email.

func (*Rand) FirstName

func (r *Rand) FirstName(gender int) string

FirstName returns a random first name, gender decides the gender of the name.

func (*Rand) Float64

func (r *Rand) Float64() float64

Float64 returns, as a float64, a pseudo-random number in the half-open interval [0.0,1.0).

func (*Rand) FullDate

func (r *Rand) FullDate() string

FullDate returns full date.

func (*Rand) FullDateInRange

func (r *Rand) FullDateInRange(dateRange ...string) string

FullDateInRange returns a date string within a given range, given in the format "2006-01-02". If no argument is supplied it will return the result of randomdata.FullDate(). If only one argument is supplied it is treated as the max date to return. If a second argument is supplied it returns a date between (and including) the two dates. Returned date is in format "Monday 2 Jan 2006".

func (*Rand) FullName

func (r *Rand) FullName(gender int) string

FullName returns a combination of FirstName LastName randomized, gender decides the gender of the name.

func (*Rand) GenerateProfile

func (r *Rand) GenerateProfile(gender int) *Profile

GenerateProfile generates a full profile.

func (*Rand) Intn

func (r *Rand) Intn(n int) int

Intn returns, as an int, a non-negative pseudo-random number in the half-open interval [0,n). It panics if n <= 0.

func (*Rand) IpV4Address

func (r *Rand) IpV4Address() string

IpV4Address returns a valid IPv4 address as string.

func (*Rand) IpV6Address

func (r *Rand) IpV6Address() string

IpV6Address returns a valid IPv6 address as net.IP.

func (*Rand) LastName

func (r *Rand) LastName() string

LastName returns a random last name.

func (*Rand) Letters

func (r *Rand) Letters(letters int) string

Letters generates a string of N random leters (A-Z).

func (*Rand) Locale

func (r *Rand) Locale() string

Locale returns a random locale.

func (*Rand) MacAddress

func (r *Rand) MacAddress() string

MacAddress returns an mac address string.

func (*Rand) Month

func (r *Rand) Month() string

Month returns random month.

func (*Rand) Noun

func (r *Rand) Noun() string

Noun returns a random noun.

func (*Rand) Number

func (r *Rand) Number(numberRange ...int) int

Number returns a random number, if only one integer (n1) is supplied it returns a number in [0,n1). if a second argument is supplied it returns a number in [n1,n2).

func (*Rand) Paragraph

func (r *Rand) Paragraph() string

Paragraph returns a random paragraph.

func (*Rand) PhoneNumber

func (r *Rand) PhoneNumber() string

PhoneNumber returns a random phone number.

func (*Rand) PostalCode

func (r *Rand) PostalCode(countrycode string) string

PostalCode yields a random postal/zip code for the given 2-letter country code.

These codes are not guaranteed to refer to actually locations. They merely follow the correct format as far as letters and digits goes. Where possible, the function enforces valid ranges of letters and digits.

func (*Rand) ProvinceForCountry

func (r *Rand) ProvinceForCountry(countrycode string) string

ProvinceForCountry returns a randomly selected province (state, county,subdivision ) name for a supplied country. If the country is not supported it will return an empty string.

func (*Rand) RandStringRunes

func (r *Rand) RandStringRunes(n int) string

RandStringRunes generates random runes.

func (*Rand) SillyName

func (r *Rand) SillyName() string

SillyName returns a silly name, useful for randomizing naming of things.

func (*Rand) State

func (r *Rand) State(typeOfState int) string

State returns a random american state.

func (*Rand) Street

func (r *Rand) Street() string

Street returns a random fake street name.

func (*Rand) StreetForCountry

func (r *Rand) StreetForCountry(countrycode string) string

StreetForCountry returns a random fake street name typical to the supplied country. If the country is not supported it will return an empty string.

func (*Rand) StringFrom

func (r *Rand) StringFrom(source []string) string

StringFrom returns a random element of a slice.

func (*Rand) StringNumber

func (r *Rand) StringNumber(numberPairs int, separator string) string

StringNumber returns a random number as a string.

func (*Rand) StringNumberExt

func (r *Rand) StringNumberExt(numberPairs int, separator string, numberOfDigits int) string

StringNumberExt returns a random number as a string.

func (*Rand) Time

func (r *Rand) Time(t time.Time, timeRange time.Duration) time.Time

Time returns a random time between the given time and within a given duration time range.

func (*Rand) TimeRange

func (r *Rand) TimeRange(t time.Time, timeRange time.Duration) (time.Time, time.Time)

TimeRange returns a random time interval between the given time and within a given time range.

func (*Rand) Timezone

func (r *Rand) Timezone() string

Timezone returns a random timezone.

func (*Rand) Title

func (r *Rand) Title(gender int) string

Title returns a random title, gender decides the gender of the name.

func (*Rand) UserAgentString

func (r *Rand) UserAgentString() string

UserAgentString returns a user agent used to browse the web.

Jump to

Keyboard shortcuts

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