Created
October 27, 2015 10:45
-
-
Save cuixin/f3431b8efb0b3d8d38e6 to your computer and use it in GitHub Desktop.
Using golang easier to generate only letters by random.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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