Debian Codegraph @

random.go

raw symbols SCIP
1 package tygoja
2
3 import (
4 mathRand "math/rand"
5 "time"
6 )
7
8 const defaultRandomAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
9
10 func init() {
11 mathRand.Seed(time.Now().UnixNano())
12 }
13
14 // PseudorandomString generates a pseudorandom string from the default
15 // alphabet with the specified length.
16 func PseudorandomString(length int) string {
17 return PseudorandomStringWithAlphabet(length, defaultRandomAlphabet)
18 }
19
20 // PseudorandomStringWithAlphabet generates a pseudorandom string
21 // with the specified length and characters set.
22 func PseudorandomStringWithAlphabet(length int, alphabet string) string {
23 b := make([]byte, length)
24 max := len(alphabet)
25
26 for i := range b {
27 b[i] = alphabet[mathRand.Intn(max)]
28 }
29
30 return string(b)
31 }