英文:
How to extract character from array in golang?
问题
需要从字符串中提取随机字符。
这是我得到的代码:
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, 1)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
fmt.Println(string(b))
但它总是返回 "X",我希望每次使用 rand 函数返回一个新字符。
非常感谢任何帮助。谢谢。
英文:
need to extract the random characters from string
here is what i got:
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, 1)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
fmt.Println(string(b))
but it always returns "X"
but, i need to return every time new character using rand function.
any help would be really appreciated.
Thanks.
答案1
得分: 2
开始通过种子生成伪随机数生成器。例如,
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, 7)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
fmt.Println(string(b))
}
输出:
jfXtySC
> Go Playground
>
> 关于 Playground
>
> 在 Playground 中,时间从 2009-11-10 23:00:00 UTC 开始
> (决定这个日期的重要性是读者的练习)。这样做可以通过给程序提供确定性输出来更容易地缓存程序。
因此,在 Go Playground 中,time.Now().UnixNano()
总是返回相同的值。要获得随机的种子值,请在您的计算机上运行代码。
对于任何 Unicode 代码点(Go rune),
package main
import (
"fmt"
"math/rand"
"time"
)
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
chars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ世界!@=")
r := make([]rune, 50)
for i := range r {
r[i] = chars[rand.Intn(len(chars))]
}
fmt.Println(string(r))
}
输出:
世QRYSp=@giJMIKly=tXRefjtVkeE!yHhTSQHvLyUYdRNIBbILW
英文:
Start by seeding the pseudorandom number generator. For example,
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, 7)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
fmt.Println(string(b))
}
Output:
jfXtySC
> The Go Playground
>
> About the Playground
>
> In the playground the time begins at 2009-11-10 23:00:00 UTC
> (determining the significance of this date is an exercise for the
> reader). This makes it easier to cache programs by giving them
> deterministic output.
Therefore, in the Go playground, time.Now().UnixNano()
always returns the same value. For a random seed value, run the code on your computer.
For any Unicode code point (Go rune),
package main
import (
"fmt"
"math/rand"
"time"
)
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
chars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ世界!@=")
r := make([]rune, 50)
for i := range r {
r[i] = chars[rand.Intn(len(chars))]
}
fmt.Println(string(r))
}
Output:
世QRYSp=@giJMIKly=tXRefjtVkeE!yHhTSQHvLyUYdRNIBbILW
答案2
得分: 0
你可能不需要使用cryto/rand。只需每次将种子设置为不同的值即可。math/rand包在相同的种子下是确定性的,所以你只需要每次改变种子。假设这不是非常安全的情况下,只需添加以下代码:
rand.Seed(time.Now().Unix())
另外请注意,如果你在play.golang.org上测试,这段代码将无法工作,因为time包在那里无法使用。但在其他地方应该可以正常工作。
如果你确实需要随机选择字符,可以使用crypto/rand.Read而不是自己编写的版本,如果需要的话,可以对其进行编码以在给定的字符范围内使用。最后,不要自己编写加密算法,以防你在这里做的就是这个。
英文:
You probably don't need cryto/rand. Just set the seed to something different each time. The math/rand package is deterministic with the same seed, so you just need to change the seed each time. Assuming this is not super secure, just add this line.
rand.Seed(time.Now().Unix())
Also note if you're using play.golang.org to test it that's not going to work because the time pkg doesn't work there. It'll work fine anywhere else.
If you actually want a random selection of characters though, use crypto/rand.Read NOT a homemade version - you can encode it if you need it within a given character range. Finally, don't roll your own crypto, just in case that's what you're doing here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论