I need to generate an unique random number in Golang. I have a simple ruby code for it:
(0...16).map { rand(10).to_s }.join
So, effectively i need to generate a number of length 16 where each digit is randomly picked up from [0-9]. I did not understand how random.Intn(n) function can help me. Any idea how can I do this?
One way is:
s := ""
for i := 0; i < 16; i++ {
s += (string)(rand.Intn(10) + 48)
}
48 is the ascii value for 0
.
Or by using @Flimzy's more efficient suggestion:
s := fmt.Sprintf("%016d", rand.Int63n(1e16))
where "%016d"
will help pad the number with zeros.