从crypto/rand生成的随机64位整数

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

Random 64-bit integer from crypto/rand

问题

我想使用安全的crypto/rand包生成64位的随机整数。我在网上找到了这个代码:

package main

import (
    "crypto/rand"
    "encoding/base64"
)

// GenerateRandomBytes返回安全生成的随机字节。
// 如果系统的安全随机数生成器无法正常工作,它将返回错误,
// 在这种情况下,调用者不应继续。
func GenerateRandomBytes(n int) ([]byte, error) {
    b := make([]byte, n)
    _, err := rand.Read(b)
    // 注意,只有当我们读取了len(b)字节时,err == nil。
    if err != nil {
        return nil, err
    }

    return b, nil
}

但它似乎只生成随机字节,而不是随机的64位整数。我想要一个随机的64位整数,类似于var i uint64 = rand()。有什么办法可以实现这个吗?

英文:

I want to generate 64-bit random integer using the secure crypto/rand package. I found this online:

package main

import (
    "crypto/rand"
    "encoding/base64"
)

// GenerateRandomBytes returns securely generated random bytes.
// It will return an error if the system's secure random
// number generator fails to function correctly, in which
// case the caller should not continue.
func GenerateRandomBytes(n int) ([]byte, error) {
    b := make([]byte, n)
    _, err := rand.Read(b)
    // Note that err == nil only if we read len(b) bytes.
    if err != nil {
        return nil, err
    }

    return b, nil
}

But it seems to generate random bytes instead. I want a random 64-bit int. Namely, I want something like var i uint64 = rand(). Any ideas how to achieve this?

答案1

得分: 3

你可以使用crypto.Rand生成一个随机数,然后使用binary包将这些字节转换为int64类型:

func randint64() (int64, error) {
    var b [8]byte
    if _, err := rand.Read(b[:]); err != nil {
        return 0, err
    }
    return int64(binary.LittleEndian.Uint64(b[:])), nil
}

如果你查看LittleEndian.Uint64的源代码,你会发现它只是对数据进行了一些位操作,这是你自己可以实现的。

英文:

You can generate a random number with crypto.Rand, and then convert those bytes to an int64 using the binary package:

func randint64() (int64, error) {
	var b [8]byte
	if _, err := rand.Read(b[:]); err != nil {
		return 0, err
	}
	return int64(binary.LittleEndian.Uint64(b[:])), nil
}

https://play.golang.org/p/2Q8tvttqbJ (result is cached)

If you look at the source code for LittleEndian.Uint64, you can see it's simply performing a few bit operations on the data; something that you could implemented for yourself.

答案2

得分: 1

你还可以在crypto/rand包中使用rand.Int

func randint64() (int64, error) {
    val, err := rand.Int(rand.Reader, big.NewInt(int64(math.MaxInt64)))
    if err != nil {
        return 0, err
    }
    return val.Int64(), nil
}

链接:https://play.golang.org/p/fqoQxpmjOSu

英文:

You also use rand.Int in crypto/rand package

func randint64() (int64, error) {
	val, err := rand.Int(rand.Reader, big.NewInt(int64(math.MaxInt64)))
	if err != nil {
		return 0, err
	}
	return val.Int64(), nil
}

https://play.golang.org/p/fqoQxpmjOSu

huangapple
  • 本文由 发表于 2017年6月11日 18:24:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/44482738.html
匿名

发表评论

匿名网友

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

确定