github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/helpers/randstring.go (about)

     1  package helpers
     2  
     3  import (
     4  	"math/rand"
     5  	"time"
     6  )
     7  
     8  // The following is ispired by https://stackoverflow.com/a/31832326
     9  func init() {
    10  	rand.Seed(time.Now().UnixNano())
    11  }
    12  
    13  var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    14  
    15  // RandomString generates an alphabetic string with uppercase and lowercase
    16  // letters of size n with some chance of being unique
    17  func RandomString(n int) string {
    18  	return RandomStringRunes(n, letterRunes)
    19  }
    20  
    21  // RandomString generates an alphabetic string with uppercase and lowercase
    22  // letters of size n with some chance of being unique
    23  func RandomStringRunes(n int, runes []rune) string {
    24  	b := make([]rune, n)
    25  	for i := range b {
    26  		b[i] = runes[rand.Intn(len(runes))]
    27  	}
    28  	return string(b)
    29  }