Golang:
func RandomString(n int) string {
var letters = []byte("ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789")
result := make([]byte, n)
//设置随机数种⼦,加上这⾏代码,可以保证每次随机都是随机
rand.Seed(time.Now().Unix())
for i := range result {
//如果不加上述 rand.Seed(time.Now().Unix())每次遍历获取都是重复的一些随机数据
result[i] = letters[rand.Intn(len(letters))]
}
//log.Println(result)
return string(result)
}
Python:
import random
def RandomString(n: int):
"""
生成 随机字符串
"""
result = ''
letters = 'ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789'
length = len(letters) - 1
for i in range(n):
result += letters[random.randint(0, length)]
return result
if __name__ == '__main__':
print(RandomString(16))
标签:__,rand,letters,Python,Golang,result,字符串,随机
From: https://www.cnblogs.com/zichliang/p/16815330.html