Comments (6)
@rdelval can write an example of how you use faker
in multiple goroutines?
from faker.
on this topic I've found this discussion
from faker.
Sure, the following should trigger
panic: runtime error: index out of range
goroutine 6 [running]:
math/rand.(*rngSource).Uint64(...)
/Users/<username>/.gvm/gos/go1.9.4/src/math/rand/rng.go:246
math/rand.(*rngSource).Int63(0xc420083500, 0x4519710ecadc72e8)
/Users/<username>.gvm/gos/go1.9.4/src/math/rand/rng.go:231 +0x8a
github.com/bxcodec/faker.randomString(0x19, 0x18, 0xc420083500)
/Users/r<username>/go/src/github.com/bxcodec/faker/faker.go:299 +0x10c
github.com/bxcodec/faker.getValue(0x1178c40, 0x10c7780, 0x10c7780, 0xc420010360, 0x16, 0x98, 0x0)
/Users/<username>/go/src/github.com/bxcodec/faker/faker.go:182 +0x1710
github.com/bxcodec/faker.FakeData(0x10c3f40, 0xc420010360, 0x0, 0x0)
/Users/ <username>/go/src/github.com/bxcodec/faker/faker.go:131 +0x1d6
main.main.func1(0xc42001a2a0, 0xc420010360)
/Users/<username>/testarea/test.go:21 +0x6a
created by main.main
/Users/<username>/testarea/test.go:17 +0x9c
exit status 2
package main
import (
"sync"
"github.com/bxcodec/faker"
)
func main() {
var wg sync.WaitGroup
for i := 0; i < 40; i++ {
wg.Add(1)
go func() {
defer wg.Done()
// Need a fixed number of strings in an array
var name string
lst := make([]string, 0, 1000)
for x := 0; x < 1000; x++ {
faker.FakeData(&name)
lst = append(lst, name)
}
}()
}
wg.Wait()
return
}
If you run this code with the -race
flag as well you'll see where the race conditions are.
Thanks for the link, hopefully Go 2 solves this issue. I was able to get this running with the default source without any race conditions. Only issue is the randomness is decreased due to the seed always being the same.
from faker.
We can add methods from native package and set seed
rand.Seed(time.Now().UnixNano())
as suggested in the discussion.
I put this entry in the general function(FakeData
)
func FakeData(a interface{}) error {
rand.Seed(time.Now().UnixNano())
// ...
// excerpt
func randomString(n int) string {
b := make([]byte, n)
for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
I did not notice that he did the same
ZwbxVNpRjFBVyWfARJTCZRKqS WQJphBDMjQZmYkmrYVgHJWVkM ZrIWzJKnMQrxIUXAuvFaCZFHF zLTjnSfNjILIQfjsXnmakPEAa JExvPuGysQIoZnSlqlCdUGbat
@rdelval Do you have any suggestions?
You or I can send PR, just let us know)
from faker.
In this case, to keep the pseudo-randomess it's best to keep a single seed set at the beginning of the initialization and use the default source throughout the rest of the program.
I'll raise a PR and we can continue the discussion there.
Thanks for looking into this and being receptive to feedback!
from faker.
PR #23
from faker.
Related Issues (20)
- Hangs up for large struct
- Feat: Generate string from regular expression
- Facebook account
- boundary_start and boundary_end for Date fields?
- Prefix and Suffix for fake string?
- Nullable pointer
- Oneof don't work with *int HOT 3
- support custom random sources
- Correct RandomInt function
- Max depth parameter
- Please add (again) support for custom random sources HOT 1
- Oneof support for alias types? HOT 2
- Tag unsupported: boundary_start=31,boundary_end=88 HOT 2
- Is bitcoin address supported? HOT 1
- `slice_len` only works for `string`
- Compatibility with go 1.18 HOT 2
- Make possible to ignore some fields or provide custom generators HOT 2
- Is there a way to make generated data optional? HOT 1
- fix panic on using map with value of aliased type
- Custom Data Make
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from faker.