Last active
December 5, 2023 08:25
-
-
Save axiaoxin/7a7fc42efd5fc0141470b49c9b82e570 to your computer and use it in GitHub Desktop.
使用golang实现一个函数,功能是生成一串6位数的随机数字字符串
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
/*使用golang实现一个函数,功能是生成一串6位数的随机数字字符串*/ | |
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
) | |
func main() { | |
digits := []rune("0123456789") | |
seed := rand.NewSource(time.Now().UnixNano()) // create a random number generator with the seed | |
rng := rand.New(seed) | |
// create a slice to store the result | |
keyLen := 6 | |
result := make([]rune, keyLen) | |
// loop n times and append a random digit to the result | |
for i := range result { | |
result[i] = digits[rng.Intn(len(digits))] | |
} | |
fmt.Println(string(result)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment