- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要一个方法,它返回从最小值到最大值的随机数,包括这两个数字。我在文章 .NET Matters: Tales from the CryptoRandom 中找到了一些代码来自 Stephen Toub 和 Shawn Farkas,该方法看起来像这样:
// Note - maxValue is excluded!
public static int GetRandomIntBetween(int minValue, int maxValue)
{
if (minValue > maxValue) throw new ArgumentOutOfRangeException("minValue");
if (minValue == maxValue) return minValue;
var rng = new RNGCryptoServiceProvider();
var uint32Buffer = new byte[4];
long diff = maxValue - minValue;
while (true)
{
rng.GetBytes(uint32Buffer);
uint rand = BitConverter.ToUInt32(uint32Buffer, 0);
const long max = (1 + (long)int.MaxValue);
long remainder = max % diff;
if (rand < max - remainder)
{
return (int)(minValue + (rand % diff));
}
}
}
我试图使 maxValue 包含在内:
public static int GetRandomIntBetween(int minValue, int maxValue)
{
if (minValue > maxValue) throw new ArgumentOutOfRangeException("minValue");
if (minValue == maxValue) return minValue;
// Make maxValue inclusive.
maxValue++;
var rng = new RNGCryptoServiceProvider();
var uint32Buffer = new byte[4];
long diff = maxValue - minValue;
while (true)
{
rng.GetBytes(uint32Buffer);
uint rand = BitConverter.ToUInt32(uint32Buffer, 0);
const long max = (1 + (long)int.MaxValue);
long remainder = max % diff;
if (rand < max - remainder)
{
return (int)(minValue + (rand % diff));
}
}
}
看起来很奇怪,但似乎我可以保持前两个检查不变,即使语义略有不同,它仍然有效。结果数据看起来也不错。我是不是漏掉了什么或者找零钱可以吗?
PS - 我问这个是因为生成随机数显然是一件非常微妙的事情,我想确保我的方法是正确的。
最佳答案
Look at my solution (click there)
您可以向类中添加其他方法:
public int NextInclusive(int minValue, int maxValue) {
return Next(minValue, maxValue + 1);
}
关于c# - 包含最小值和最大值的 RNGCryptoServiceProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14935708/
我正在将我们的单元测试从 Moles 转换为新的 VS 2012 Fakes。我们的几个单元测试是“假的”RNGCryptoServiceProvider。我们能够“挖”出这个,但假货中似乎没有为此创
有许多经过验证且完善的伪随机数生成算法可供使用,以及其他最近受到审查的算法;我想知道 .Net 4.0 RNGCryptoServiceProvider 使用哪种算法? 在启动正在进行的项目之前,我很
我正在将我们的单元测试从 Moles 转换为新的 VS 2012 Fakes。我们的几个单元测试“伪造”RNGCryptoServiceProvider。我们能够“挖”出来,但似乎在 Fakes 中没
我正在寻找一种获取随机字符的方法。我需要一个字符串,该字符串必须至少包含 2 个大写字母、至少 1 个数字和特殊字符。这是我的代码: public static string CreateRandom
我需要一个方法,它返回从最小值到最大值的随机数,包括这两个数字。我在文章 .NET Matters: Tales from the CryptoRandom 中找到了一些代码来自 Stephen To
有谁知道为什么 RNGCryptoServiceProvider 在尝试获取大于 300,000,000 的数字时无法通过卡方检验。 我尝试获取 0-1,000,000,000 范围内的随机数,结果卡
我正在使用 RNG 加密提供程序以真正天真的方式生成一定范围内的数字: byte[] bytes = new byte[4]; int result = 0; while(result max) {
在寻找生成真正随机数的最佳尝试时,我偶然发现了这个代码示例。 寻找关于这个片段的意见。 using System; using System.Security.Cryptography; privat
使用 System.Security.Cryptography.RNGCryptoServiceProvider 与 System.Random 的优缺点是什么。我知道 RNGCryptoServic
我使用以下代码生成 1000-9999 范围内的代码: Dim byt As Byte() = New Byte(1000) {} Dim rngCrypto As New RNGCryptoServ
RNGCryptoServiceProvider.GetBytes 线程安全吗?我似乎在文档中找不到任何关于它的注释。 最佳答案 是的。这是in the "remarks" section : The
我使用以下代码生成 1000-9999 范围内的代码: Dim byt As Byte() = New Byte(1000) {} Dim rngCrypto As New RNGCryptoServ
我在编码时看到了这个,觉得它很奇怪: 果然,MSDN表示 RNGCryptoServiceProvider(byte[] rgb) 和 RNGCryptoServiceProvider(string
通过 Rfc2898DeriveBytes.GetBytes() 运行由 RNGCryptoServiceProvider 生成的加密 key (2048 位)是否有意义,或者是 Rfc2898Der
我正在将一些 C# 代码转换为 Java。我找不到与 RNGCryptoServiceProvider 等效的方法。我该怎么做? private static String GetRandomSalt
根据 RandomNumberGenerator 的 MSDN 文档: Application code does not directly use this class. This abstract
桥牌游戏使用 52 张不同的扑克牌进行,这些牌随机分配给四名玩家,每名玩家最终得到 13 张牌:即所谓的“发牌”。 Roughly a little less than 2^96 Bridge dea
这是我的方法: public static string GenerateRandomString(int bytes) { var rng = new RNGCryptoServicePro
浏览一些密码学的东西,我看到 RNGCryptoServiceProvider 有 2 个方法: link RNGCryptoServiceProvider.GetNonZeroBytes 和 RNG
我正在使用这段代码生成给定长度的随机字符串 public string RandomString(int length) { const string valid = "abcdefghijk
我是一名优秀的程序员,十分优秀!