How to generate a variable length random number in Go

huangapple go评论69阅读模式
英文:

How to generate a variable length random number in Go

问题

我正在尝试在Go中生成一个长度可变的随机整数,但我总是得到填充了总长度的数字。以下是我的代码:

package main

import (
    "fmt"
    "math/big"
    "crypto/rand"
)

const ResultsPerPage = 30

var (
        total = new(big.Int).SetBytes([]byte{
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
                0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48, 0xA0, 0x3B, 0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41, 0x40,
        })
        pages = new(big.Int).Div(total, big.NewInt(ResultsPerPage))
        random *big.Int
        err error
)

func main() {

fmt.Println(pages)
fmt.Println(randomString(pages))
}

func randomString(l *big.Int) string {
  random, err = rand.Int(rand.Reader, l)
  fmtrandom := fmt.Sprint(random)
  return string(fmtrandom)
}

输出结果:

3859736307910539847452366166956263595094585475969163479420172104717272049811
3479662380009045046388212253547512051795238437604387552617121977169155584415

代码示例:https://play.golang.org/p/HxAb6Rs_Uj

感谢任何帮助。谢谢。

英文:

I'm trying to generate a random integer with variable length in Go but I always get the number filling the total length. Here's my code:

package main

import (
    "fmt"
    "math/big"
    "crypto/rand"
)

const ResultsPerPage = 30

var (
        total = new(big.Int).SetBytes([]byte{
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
                0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48, 0xA0, 0x3B, 0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41, 0x40,
        })
        pages = new(big.Int).Div(total, big.NewInt(ResultsPerPage))
        random *big.Int
        err error
)

func main() {

fmt.Println(pages)
fmt.Println(randomString(pages))
}

func randomString(l *big.Int) string {
  random, err = rand.Int(rand.Reader, l)
  fmtrandom := fmt.Sprint(random)
  return string(fmtrandom)
}

Outputs

3859736307910539847452366166956263595094585475969163479420172104717272049811
3479662380009045046388212253547512051795238437604387552617121977169155584415

Code sample: https://play.golang.org/p/HxAb6Rs_Uj

Any help is appreciated. Thank you.

答案1

得分: 2

如果我理解正确,你遇到的问题是随机数似乎总是与允许的最大值具有相同的位数,对吗?当我运行代码时,情况并非如此。这是我刚刚观察到的输出:

3859736307910539847452366166956263595094585475969163479420172104717272049811
65719900872761032562423535702578352960653752260368991759410130265294153783

在 playground 中,我相信随机种子和时钟时间是固定的,所以你会反复看到相同的结果。但在本地运行时,我看到了预期的输出长度变化。

更新

你可能想知道为什么你可以多次运行这段代码,并观察到随机数的长度很常常接近最大值的长度。

一点数学可以解释这个问题。让我们想象我们在0到999之间选择一个数字。在这个范围内有多少个数字:

  1. 有3位数?900个(100-999)
  2. 有2位数?90个(10-99)
  3. 有1位数?10个(0-9)

类似地,对于你的非常大的最大值,大多数可以选择的数字将非常接近该最大长度。很少会看到比该长度短两位数的数字。(你应该大约1%的时间看到它们。)

英文:

If I understand correctly, the issue you're having is that the random number appears to always have the same number of digits as the maximum allowed value, right? When I run the code, this is not the case. Here's an output I just observed:

3859736307910539847452366166956263595094585475969163479420172104717272049811
65719900872761032562423535702578352960653752260368991759410130265294153783

In the playground, I believe the random seed and clock time are fixed, so you'll see the same result repeatedly. But running locally, I see the expected variation in the length of the output.

UPDATE

You may wonder why you can run this code a bunch of times and observe that the length of the random number is very often close to the length of the maximum value.

A little math explains this. Let's imagine we're picking a number between 0 and 999. How many numbers in that range have:

  1. 3 digits? 900 (100-999)
  2. 2 digits? 90 (10-99)
  3. 1 digit? 10 (0-9)

Similarly, with your very large maximum value, most of the numbers that can be picked will be very close to that maximum length. It will be rare to see a number two digits short of that length. (You should see them on the order of 1% of the time.)

huangapple
  • 本文由 发表于 2017年6月27日 15:27:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/44774514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定