random.go 671 B

12345678910111213141516171819202122
  1. // Copyright 2025 BackendServerTemplate Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package randomutils
  5. import (
  6. "math/rand"
  7. "time"
  8. )
  9. // GenerateRandomString generates a random string of the specified length
  10. // containing lowercase letters ('a'-'z') and digits ('0'-'9').
  11. func GenerateRandomString(length int) string {
  12. const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
  13. seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
  14. result := make([]byte, length)
  15. for i := range result {
  16. result[i] = charset[seededRand.Intn(len(charset))]
  17. }
  18. return string(result)
  19. }