pallinder / go-randomdata Goto Github PK
View Code? Open in Web Editor NEWA tiny generator of random data for golang, also known as a faker
License: MIT License
A tiny generator of random data for golang, also known as a faker
License: MIT License
Hi,
Could you add a sentence generator?When generating some articles ,title and content are needed,content can be generated by randomdata.Paragraph()
,but I don't find which one should be used for generating title.
For example:
// Print a sentence
fmt.Println(randomdata.Sentence())
I noticed that you explicitly do rand.Seed(time.Now().UnixNano())
in each call to randomFrom
.
Sometimes it is desirable to have a fixed seed for more reliable and predictable unit tests behaviour. Would it be possible to seed the randomiser only once, during package init and store the used seed? Additionally add a function that allows one to manually set the seed. Something like:
var seed int64
func Seed() int64 {
return seed
}
func SetSeed(v int64) {
seed = v
rand.Seed(v)
}
func init() {
SetSeed(time.Now().UnixNano())
}
followup to #67
package level globals is a bit of an antipattern, but can definitely be used for simplified use cases. In the case of tests and parallelism and reproducibility, I'd prefer if I had a randomdata.New()
that I could rely on, with an optional input variable of the random seed.
Thanks!
randomdata.Paragraph()
Seems to output very often almost the same sentance .
It would be good if we could have a bigger variety.
For example when testing big data , with full text search, having more words to scan would be nice
I leverage the randomdata.FullDate()
function call in some unit tests by parsing the random full dates into time.Time structs. :
t, err = time.Parse(dateLayout, randomdata.FullDate())
if err != nil {
Fail(err.Error())
}
What I expected to see : The above fail handler would not be called
What I actually saw : parsing time "Tuesday 29 Feb 2018": day out of range
This causes intermittent unit test failures.
生成的随机数,重复的几率很大。
If someone uses this in tests, the inability to seed the random number generator will cause unreproducible test flakiness.
Please consider adding support for ICU Locales
preferrably phone numbers in some E.164 Format(s)
see https://en.wikipedia.org/wiki/E.164
I think this should be self explanatory. First wanna propose this before making a PR though.
Can read more here: https://github.com/golang/go/wiki/Modules
goroutine 9 [running]:
math/rand.(*rngSource).Uint64(...)
/usr/local/Cellar/go/1.10.3/libexec/src/math/rand/rng.go:246
math/rand.(*rngSource).Int63(0xc4200a5500, 0xc4202e8bd0)
/usr/local/Cellar/go/1.10.3/libexec/src/math/rand/rng.go:231 +0x8a
math/rand.(*Rand).Int63(0xc420094510, 0x4)
/usr/local/Cellar/go/1.10.3/libexec/src/math/rand/rand.go:82 +0x33
math/rand.(*Rand).Int31(0xc420094510, 0x4)
/usr/local/Cellar/go/1.10.3/libexec/src/math/rand/rand.go:96 +0x2b
math/rand.(*Rand).Int31n(0xc420094510, 0x20f, 0x0)
/usr/local/Cellar/go/1.10.3/libexec/src/math/rand/rand.go:131 +0x4f
math/rand.(*Rand).Intn(0xc420094510, 0x20f, 0x0)
/usr/local/Cellar/go/1.10.3/libexec/src/math/rand/rand.go:169 +0x45
github.com/jayunit100/vuln-sim/vendor/github.com/Pallinder/go-randomdata.randomFrom(0xc4200d0000, 0x20f, 0x2c7, 0x0, 0xc4202e8b48)
/Users/jayunit100/go/src/github.com/jayunit100/vuln-sim/vendor/github.com/Pallinder/go-randomdata/random_data.go:84 +0x37
github.com/jayunit100/vuln-sim/vendor/github.com/Pallinder/go-randomdata.Adjective(0xc4200192d8, 0x4)
/Users/jayunit100/go/src/github.com/jayunit100/vuln-sim/vendor/github.com/Pallinder/go-randomdata/random_data.go:262 +0x45
github.com/jayunit100/vuln-sim/vendor/github.com/Pallinder/go-randomdata.SillyName(0x7ffffff7010ae51b, 0xc4202e8c68)
/Users/jayunit100/go/src/github.com/jayunit100/vuln-sim/vendor/github.com/Pallinder/go-randomdata/random_data.go:279 +0x44
github.com/jayunit100/vuln-sim/pkg/model3.randApp(0xa, 0xc420106080, 0xc4202e8d20, 0x1011519, 0xc5f68aade0)
/Users/jayunit100/go/src/github.com/jayunit100/vuln-sim/pkg/model3/simulator.go:154 +0x26
github.com/jayunit100/vuln-sim/pkg/model3.(*ClusterSim).initEvents.func1(0xc4200a92c0, 0xc5f68aade0, 0xc4202e8e88, 0x4, 0xc5f68a1500)
/Users/jayunit100/go/src/github.com/jayunit100/vuln-sim/pkg/model3/simulator.go:230 +0x22e
github.com/jayunit100/vuln-sim/pkg/model3.(*ClusterSim).initEvents(0xc4200a92c0, 0xc422f7a060, 0xc4239619c0, 0xa)
/Users/jayunit100/go/src/github.com/jayunit100/vuln-sim/pkg/model3/simulator.go:236 +0x59d
github.com/jayunit100/vuln-sim/pkg/model3.(*ClusterSim).Initialize(0xc4200a92c0)
/Users/jayunit100/go/src/github.com/jayunit100/vuln-sim/pkg/model3/simulator.go:203 +0x1c6
github.com/jayunit100/vuln-sim/pkg/model3.(*ClusterSim).Simulate(0xc4200a92c0, 0x0)
/Users/jayunit100/go/src/github.com/jayunit100/vuln-sim/pkg/model3/simulator.go:376 +0x34
main.ExperimentalSimulation1.func1(0xc42008a6c0, 0xc4200a92c0)
/Users/jayunit100/go/src/github.com/jayunit100/vuln-sim/clustersim/main.go:106 +0x2b
created by main.ExperimentalSimulation1
/Users/jayunit100/go/src/github.com/jayunit100/vuln-sim/clustersim/main.go:105 +0x122
Hi,
I've seen you are using math/rand, which causes diversity and gender quota issues ;-)
I suggest changing random number generation from
import "math/rand"
// ...
g := rand.Intn(2)
to
import "crypto/rand"
// ...
n, _ := rand.Int(rand.Reader, big.NewInt(2))
g := int(n.Int64())
(Respectively, something similar in func randomFrom(...)
)
I could mitigate the gender selection by generating the random number myself, but the name for that gender is then still decided by internals and not properly random, often causing the same name in a row.
While executing this operation multiple times on Windows 8.1 x64:
fmt.Println(StringNumber(4, "-"))
I get strange results:
44-44-44-44
55-55-55-55
11-11-11-11
00-00-00-00
If we look at seedAndReturnRandom() function, we can find:
rand.Seed(time.Now().UnixNano())
This probably happens because golang executes faster than nanosecond passes. Should we sleep for a while before seeding, implement different seeding algorithm or it's a platform-specific bug?
P.S. My co-worker launched this test on Gentoo, and it works properly.
Hello:
Can you add a function when given a slice of strings, will return one of them at random? Suppose the new function signature looks like String(stringList ...string) string
. Then, one of the strings in the given list is returned at random.
Here is an example invocation:
myString := randomdata.String("foo", "bar", "baz")
Thank you,
Evan McClure
Hi!
Not good idea to use rand.Seed(time.Now().UnixNano())
when generate IPv6 address.
As result: IpAddress : 6363:6363:6363:6363:6363:6363:6363:6363
Please transfer rand.Seed(time.Now().UnixNano())
to init func.
func seedAndReturnRandom(n int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(n)
}
// Returns a valid IPv6 address as net.IP
func IpV6Address() string {
var ip net.IP
for i := 0; i < net.IPv6len; i++ {
number := uint8(seedAndReturnRandom(255))
ip = append(ip, number)
}
return ip.String()
}
https://github.com/Pallinder/go-randomdata/blob/master/random_data.go#L67
See below the bug in default switch.
// Title returns a random title, gender decides the gender of the name
func Title(gender int) string {
var title = ""
switch gender {
case Male:
title = randomFrom(jsonData.MaleTitles)
break
case Female:
title = randomFrom(jsonData.FemaleTitles)
break
default:
title = FirstName(privateRand.Intn(2))
break
}
return title
}
It should be either wrapped in a lock or global rand package object must be used. Quoting rand#NewSource documentation: https://golang.org/pkg/math/rand/#NewSource
NewSource returns a new pseudo-random Source seeded with the given value. Unlike the default Source used by top-level functions, this source is not safe for concurrent use by multiple goroutines.
As the title says.
It's kinda odd to see a cool library still not being versioned even though it seems to have been around for 5 years already and going through a decent evolution.
Please consider:
CHANGELOG.md
fileThis would greatly improve the library user experience and the seriousness of the project by allowing the user to achieve reproducible builds when using dep for example.
Currently, there seems to be no guarantee on backward-compatibility so the current release would need to start with 0.1
according to semver, though if it's already production ready then start with 1.0
right away.
This issue is loosely related to #47
FR: Add a method similar to FullDate()
but with the ability to give a range for that random date to be within.
Upon checking on https://app.readable.com/text/profanity/ there are some words that may be profane.
Example on nouns object, there is a slave
word. I also noticed that there are old issue that is related on it #70.
Thanks.
Makes life easier when changing data, would also allow for people to add custom dictionaries and would allow us to support internationalization eventually.
There should be a way to generate a complete address (including postal number).
I'm currently using this package for test fixture generation
and it happens quite often that i get the same email in just few repetitive calls, which prevents from using it 'as is' and requiring to add some random part manually.
List all https://github.com/Pallinder/go-randomdata/graphs/contributors in readme?
We should support the different kinds of formats as used around the world.
Depends on #1
panic: runtime error: index out of range
goroutine 28 [running]:
math/rand.(*rngSource).Uint64(...)
/usr/local/share/go/src/math/rand/rng.go:249
math/rand.(*rngSource).Int63(0xc0000c9500, 0x4f95066a366ef45d)
/usr/local/share/go/src/math/rand/rng.go:234 +0x89
math/rand.(*Rand).Int63(0xc0000aa4e0, 0x7ffffffe78bb7a00)
/usr/local/share/go/src/math/rand/rand.go:85 +0x33
math/rand.(*Rand).Int31(0xc0000aa4e0, 0x1e)
/usr/local/share/go/src/math/rand/rand.go:99 +0x2b
math/rand.(*Rand).Int31n(0xc0000aa4e0, 0xe0000001e, 0x0)
/usr/local/share/go/src/math/rand/rand.go:134 +0x4f
math/rand.(*Rand).Intn(0xc0000aa4e0, 0x1e, 0x14d)
/usr/local/share/go/src/math/rand/rand.go:172 +0x45
github.com/Pallinder/go-randomdata.Number(0xc000297ea8, 0x2, 0x2, 0x1e)
/usr/local/share/gopath/src/github.com/Pallinder/go-randomdata/random_data.go:225 +0x83
github.com/Pallinder/go-randomdata.FullDate(0x411703e3289fc8f6, 0x45)
/usr/local/share/gopath/src/github.com/Pallinder/go-randomdata/random_data.go:358 +0x10d
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.