Skip to content

Instantly share code, notes, and snippets.

@cuixin
Created October 27, 2015 10:45
Show Gist options
  • Save cuixin/f3431b8efb0b3d8d38e6 to your computer and use it in GitHub Desktop.
Save cuixin/f3431b8efb0b3d8d38e6 to your computer and use it in GitHub Desktop.
Using golang easier to generate only letters by random.
package main
import (
"crypto/rand"
"fmt"
"io"
)
func newId(size int) string {
k := make([]byte, size)
if _, err := io.ReadFull(rand.Reader, k); err != nil {
return ""
} else {
for i, v := range k {
x := 65 + v%26
k[i] = x
}
return string(k)
}
}
func main() {
size := 10000000
var slot = make(map[string]struct{}, size)
for i := 0; i < size; i++ {
k := newId(10)
if _, ok := slot[k]; !ok {
slot[k] = struct{}{}
// fmt.Println(k)
} else {
fmt.Println("Same", k, i)
panic(k)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment