-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom-string-prefix.go
More file actions
54 lines (44 loc) · 902 Bytes
/
random-string-prefix.go
File metadata and controls
54 lines (44 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"bytes"
"crypto/rand"
"fmt"
"math/big"
)
const (
length = 6
haveLeadingDigit = false
)
func getRandomInt(max int) (num int64) {
// this is from 0 to max-1
b, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
if err != nil {
panic(err)
}
return b.Int64()
}
func sample(set []string) (item string) {
return set[getRandomInt(len(set))]
}
func main() {
letters := []string{
"b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
"n", "p", "q", "r", "s", "t", "v", "w", "x", "z",
}
digits := []string{
"0", "1", "2", "3", "5", "6", "7", "9",
}
all := append(letters, digits...)
var buf bytes.Buffer
if haveLeadingDigit {
for range make([]int, length) {
buf.WriteString(sample(all))
}
} else {
buf.WriteString(sample(letters))
for range make([]int, length-1) {
buf.WriteString(sample(all))
}
}
fmt.Println(buf.String())
}