golang 字符串随机数_在Go中生成随机数和字符串

golang 字符串随机数

While completely random is not really possible, we still can have pseudorandom numbers on computers.

尽管不可能完全随机 ,但我们仍然可以在计算机上使用伪随机数。

We can have regular pseudorandom numbers, and cryprographically secure pseudorandom numbers.

我们可以有规则的伪随机数,也可以有安全的伪随机数。

Let’s see how to that in Go.

让我们看看如何在Go中做到这一点。

伪随机数 (Pseudorandom numbers)

The math/rand package provided by the Go Standard Library gives us pseudo-random number generators (PRNG), also called deterministic random bit generators.

Go标准库提供的math/rand软件包为我们提供了伪随机数生成器(PRNG) ,也称为确定性随机位生成器

As with all pseudo number generators, any number generated through math/rand is not really random by default, as being deterministic it will always print the same value each time.

与所有伪数字生成器一样,默认情况下,通过math/rand生成的任何数字都不真正随机的,因为具有确定性,它将始终每次打印相同的值

As an example, try running this code which introduces rand.Intn(n), which returns a random number between 0 and n - 1.

例如,请尝试运行引入rand.Intn(n)代码,该代码返回0n - 1之间的随机数。

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    fmt.Println(rand.Intn(30))
    fmt.Println(rand.Intn(30))
    fmt.Println(rand.Intn(30))
    fmt.Println(rand.Intn(30))
    fmt.Println(rand.Intn(30))
}

playground

操场

You’ll always see the same sequence every time

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值