Skip to content

Commit

Permalink
readme - updates to reflect faker changes
Browse files Browse the repository at this point in the history
  • Loading branch information
brianvoe committed Feb 18, 2024
1 parent e7c55ca commit 6c75e18
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,43 @@ gofakeit.Seed(8675309) // Set it to whatever number you want

## Random Sources

Gofakeit has a few rand sources, by default it uses math.Rand and uses mutex locking to allow for safe goroutines.
Gofakeit has a few rand sources, by default it uses math/rand/v2 PCG which is a pseudo random number generator and is thread locked.

If you want to use a more performant source please use NewUnlocked. Be aware that it is not goroutine safe.
If you want to see other potential sources you can see the sub package [Source](https://pkg.go.dev/github.com/brianvoe/gofakeit/v7/source) for more information.

```go
import "github.com/brianvoe/gofakeit/v7"
import (
"github.com/brianvoe/gofakeit/v7"
"github.com/brianvoe/gofakeit/v7/source"
"math/rand/v2"
)

// Uses math/rand/v2(Pseudo) with mutex locking
// Uses math/rand/v2(PCG Pseudo) with mutex locking
faker := gofakeit.New(0)

// Uses math/rand/v2(Pseudo) with NO mutext locking
// More performant but not goroutine safe.
faker := gofakeit.NewUnlocked(0)
// NewFaker takes in a source and whether or not it should be thread safe
faker := gofakeit.NewFaker(source rand.Source, threadSafe bool)

// PCG Pseudo
faker := gofakeit.NewFaker(rand.NewPCG(11, 11), true)

// ChaCha8
faker := gofakeit.NewFaker(rand.NewChaCha8([32]byte{0, 1, 2, 3, 4, 5}), true)


// Additional from Gofakeit sub package source

// Uses crypto/rand(cryptographically secure) with mutext locking
faker := gofakeit.NewCrypto()
// JSF(Jenkins Small Fast)
faker := gofakeit.NewFaker(source.NewJSF(11), true)

// Pass in your own random source
faker := gofakeit.NewCustom()
// SFC(Simple Fast Counter)
faker := gofakeit.NewFaker(source.NewSFC(11), true)

// Crypto - Uses crypto/rand
faker := gofakeit.NewFaker(source.NewCrypto(), true)

// Dumb - simple incrementing number
faker := gofakeit.NewFaker(source.NewDumb(11), true)
```

## Global Rand Set
Expand All @@ -103,10 +121,13 @@ If you would like to use the simple function calls but need to use something lik
crypto/rand you can override the default global with the random source that you want.

```go
import "github.com/brianvoe/gofakeit/v7"
import (
"github.com/brianvoe/gofakeit/v7"
"math/rand/v2"
)

faker := gofakeit.NewCrypto()
gofakeit.SetGlobalFaker(faker)
faker := gofakeit.New(0)
gofakeit.GlobalFaker = faker
```

## Struct
Expand Down Expand Up @@ -147,7 +168,7 @@ type Bar struct {

// Pass your struct as a pointer
var f Foo
gofakeit.Struct(&f)
err := gofakeit.Struct(&f)

fmt.Println(f.Str) // hrukpttuezptneuvunh
fmt.Println(f.Int) // -7825289004089916589
Expand Down Expand Up @@ -230,7 +251,7 @@ gofakeit.AddFuncLookup("friendname", gofakeit.Info{
Example: "bill",
Output: "string",
Generate: func(f *Faker, m *gofakeit.MapParams, info *gofakeit.Info) (any, error) {
return gofakeit.RandomString([]string{"bill", "bob", "sally"}), nil
return f.RandomString([]string{"bill", "bob", "sally"}), nil
},
})

Expand All @@ -250,7 +271,7 @@ gofakeit.AddFuncLookup("jumbleword", gofakeit.Info{
}

split := strings.Split(word, "")
gofakeit.ShuffleStrings(split)
f.ShuffleStrings(split)
return strings.Join(split, ""), nil
},
})
Expand Down Expand Up @@ -766,12 +787,7 @@ Float64() float64
Float64Range(min, max float64) float64
ShuffleInts(a []int)
RandomInt(i []int) int
HexUint8() string
HexUint16() string
HexUint32() string
HexUint64() string
HexUint128() string
HexUint256() string
HexUint(bitsize int) string
```

### String
Expand Down

0 comments on commit 6c75e18

Please sign in to comment.