我正在尝试使用RNGCryptoServiceProvider生成随机数,但有些我如何得到o(零)作为回报
private int generatePassword()
{
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
string value = string.Empty;
byte[] randomnumber = new byte[10000];
rng.GetBytes(randomnumber);
return randomnumber;
}
}
答案 0 :(得分:0)
也许你应该看看这个link here
static string RandomString(int length)
{
const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder res = new StringBuilder();
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
byte[] uintBuffer = new byte[sizeof(uint)];
while (length-- > 0)
{
rng.GetBytes(uintBuffer);
uint num = BitConverter.ToUInt32(uintBuffer, 0);
res.Append(valid[(int)(num % (uint)valid.Length)]);
}
}
return res.ToString();
}