在 C# 中生成随机值
如何使用 C# 中的 Random
类生成随机 Int64 和 UInt64 值?
How can I generate random Int64 and UInt64 values using the Random
class in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这应该可以解决问题。 (它是一个扩展方法,因此您可以像调用
Random
对象上的普通Next
或NextDouble
方法一样调用它)。如果您想要无符号整数,只需将
Int64
替换为UInt64
即可,一切都应该正常工作。注意:由于没有提供有关安全性或生成的数字所需的随机性的上下文(实际上OP特别提到了
Random
类),我的示例仅处理< code>Random 类,这是随机性时的首选解决方案(通常量化为信息熵) 不是问题。 感兴趣的是,请参阅提到RNGCryptoServiceProvider
(System.Security
命名空间中提供的 RNG)的其他答案,它的使用几乎相同。This should do the trick. (It's an extension method so that you can call it just as you call the normal
Next
orNextDouble
methods on aRandom
object).Just replace
Int64
withUInt64
everywhere if you want unsigned integers instead and all should work fine.Note: Since no context was provided regarding security or the desired randomness of the generated numbers (in fact the OP specifically mentioned the
Random
class), my example simply deals with theRandom
class, which is the preferred solution when randomness (often quantified as information entropy) is not an issue. As a matter of interest, see the other answers that mentionRNGCryptoServiceProvider
(the RNG provided in theSystem.Security
namespace), which can be used almost identically.使用
Random.NextBytes()
< /a> 和BitConverter.ToInt64
/BitConverter.ToUInt64
。请注意,使用
Random.Next()
两次,移动一个值然后进行“或”/相加不起作用。Random.Next()
仅生成非负整数,即生成 31 位,而不是 32 位,因此两次调用的结果仅生成 62 位随机位,而不是覆盖整个范围所需的 64 位Int64
/UInt64
。 (Guffa 的答案展示了如何使用三个< /em> 调用Random.Next()
。)Use
Random.NextBytes()
andBitConverter.ToInt64
/BitConverter.ToUInt64
.Note that using
Random.Next()
twice, shifting one value and then ORing/adding doesn't work.Random.Next()
only produces non-negative integers, i.e. it generates 31 bits, not 32, so the result of two calls only produces 62 random bits instead of the 64 bits required to cover the complete range ofInt64
/UInt64
. (Guffa's answer shows how to do it with three calls toRandom.Next()
though.)在这里,它使用了加密货币服务(不是
Random
类),它(理论上)是比Random类更好的RNG。 您可以轻松地将其作为 Random 的扩展,或者创建您自己的 Random 类,其中 RNGCryptoServiceProvider 是类级对象。Here you go, this uses the crytpo services (not the
Random
class), which is (theoretically) a better RNG then the Random class. You could easily make this an extension of Random or make your own Random class where the RNGCryptoServiceProvider is a class-level object.您可以使用位移位将 31 位随机数组合成一个 64 位随机数,但您必须使用三个 31 位数字才能获得足够的位:
You can use bit shift to put together a 64 bit random number from 31 bit random numbers, but you have to use three 31 bit numbers to get enough bits:
我总是用它来获取我的随机种子(为简洁起见,删除了错误检查):
random.org 使用大气噪声来生成随机性,显然用于彩票等。
I always use this to get my random seed (error checking removed for brevity):
random.org uses atmospheric noise to generate the randomness and is apparently used for lotteries and such.
您没有说明将如何使用这些随机数...请记住,Random 返回的值不是“加密安全的”,并且它们不应该用于涉及 (大)秘密或(很多)金钱。
You don't say how you're going to use these random numbers...keep in mind that values returned by Random are not "cryptographically secure" and they shouldn't be used for things involving (big) secrets or (lots of) money.
您可以创建一个
byte
数组,用随机数据填充它,然后将其转换为long
(Int64
) 或 ulong(UInt64
)。You could create a
byte
array, fill it with random data and then convert it tolong
(Int64
) or ulong (UInt64
).另一个答案是使用
RNGCryptoServiceProvider
而不是Random
。 在这里您可以看到如何删除 MSB,以便结果始终为正。Another answer with
RNGCryptoServiceProvider
instead ofRandom
. Here you can see how to remove the MSB so the result is always positive.从 .NET 6 开始,
Random
类具有生成随机 long 的方法。As of .NET 6, the
Random
class has a method for generating a random long.