如何在 Julia 中生成随机的十六进制字符串
How to generate a random hexadecimal string in Julia
我正在尝试为区块链钱包应用程序生成一个随机的十六进制字符串,但我不确定我将如何在 Julia 中执行此操作。在 Python,我会做类似
的事情
import os
from ecdsa import SigningKey, VerifyingKey, SECP256k1
seed = os.urandom(SECP256k1.baselen)
作为stated in the documentation of the standard library:
Julia also provides the RandomDevice
RNG type, which is a wrapper over the OS provided entropy.
注意这里滥用了“熵”这个词。 OS 很可能不会直接 return 熵,它将 return 来自它自己的种子伪随机数生成器的随机字节。
您可以使用 randstring
指定允许的字符:
julia> using Random
# default length is 8 characters
julia> randstring(['0':'9'; 'a':'f'])
"e7e15070"
# other custom lengths
julia> randstring(['0':'9'; 'a':'f'], 4)
"9a6a"
julia> randstring(['0':'9'; 'a':'f'], 12)
"df398edb1937"
julia> bytes2hex(rand(UInt8, 4))
"9fae741a"
rand
和 randstring
默认使用 MersenneTwister
pseudorandom number generator. If you want to use different streams of random numbers you can use RandomDevice
代替:
julia> randstring(RandomDevice(), ['0':'9'; 'a':'f'])
"6f4499e3"
julia> bytes2hex(rand(RandomDevice(), UInt8, 4))
"4d1c035c"
我正在尝试为区块链钱包应用程序生成一个随机的十六进制字符串,但我不确定我将如何在 Julia 中执行此操作。在 Python,我会做类似
的事情import os
from ecdsa import SigningKey, VerifyingKey, SECP256k1
seed = os.urandom(SECP256k1.baselen)
作为stated in the documentation of the standard library:
Julia also provides the
RandomDevice
RNG type, which is a wrapper over the OS provided entropy.
注意这里滥用了“熵”这个词。 OS 很可能不会直接 return 熵,它将 return 来自它自己的种子伪随机数生成器的随机字节。
您可以使用 randstring
指定允许的字符:
julia> using Random
# default length is 8 characters
julia> randstring(['0':'9'; 'a':'f'])
"e7e15070"
# other custom lengths
julia> randstring(['0':'9'; 'a':'f'], 4)
"9a6a"
julia> randstring(['0':'9'; 'a':'f'], 12)
"df398edb1937"
julia> bytes2hex(rand(UInt8, 4))
"9fae741a"
rand
和 randstring
默认使用 MersenneTwister
pseudorandom number generator. If you want to use different streams of random numbers you can use RandomDevice
代替:
julia> randstring(RandomDevice(), ['0':'9'; 'a':'f'])
"6f4499e3"
julia> bytes2hex(rand(RandomDevice(), UInt8, 4))
"4d1c035c"