本文整理汇总了C#中IRandom.NextBytes方法的典型用法代码示例。如果您正苦于以下问题:C# IRandom.NextBytes方法的具体用法?C# IRandom.NextBytes怎么用?C# IRandom.NextBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IRandom
的用法示例。
在下文中一共展示了IRandom.NextBytes方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetGuid
public static System.Guid GetGuid(IRandom rand)
{
byte[] buffer = new byte[16];
rand.NextBytes(buffer);
return new System.Guid(buffer);
}
示例2: GetBinary
public static System.Byte[] GetBinary(IRandom rand, int minLength, int maxLength)
{
int length = rand.Next(minLength, maxLength);
byte[] result = new byte[length];
rand.NextBytes(result);
return result;
}
示例3: GetDouble
public static System.Double GetDouble( IRandom rand,
System.Double min = System.Double.MinValue,
System.Double max = System.Double.MaxValue)
{
byte[] buffer = new byte[8];
rand.NextBytes(buffer);
System.Double result = System.BitConverter.ToDouble(buffer, 0);
if(result < min)
return min;
else if(result >= max)
return max - 1;
else
return result;
}
示例4: MeasureChiSquared
private static double MeasureChiSquared(IRandom random, int rounds)
{
var counts = new int[256];
var bs = new byte[256];
for (var i = 0; i < rounds; ++i)
{
random.NextBytes(bs);
for (var b = 0; b < 256; ++b)
{
++counts[bs[b]];
}
}
var mask = SecureRandom.GetSeed(1)[0];
for (var i = 0; i < rounds; ++i)
{
random.NextBytes(bs);
for (var b = 0; b < 256; ++b)
{
++counts[bs[b] ^ mask];
}
++mask;
}
var shift = SecureRandom.GetSeed(1)[0];
for (var i = 0; i < rounds; ++i)
{
random.NextBytes(bs);
for (var b = 0; b < 256; ++b)
{
++counts[(byte)(bs[b] + shift)];
}
++shift;
}
var total = 3 * rounds;
double chi2 = 0;
for (var k = 0; k < counts.Length; ++k)
{
var diff = ((double)counts[k]) - total;
var diff2 = diff * diff;
chi2 += diff2;
}
chi2 /= total;
return chi2;
}
示例5: BigInteger
public BigInteger(
int bitLength,
int certainty,
IRandom random)
{
if (bitLength < 2)
throw new ArithmeticException("bitLength < 2");
this.SignValue = 1;
this.nBitLength = bitLength;
if (bitLength == 2)
{
this.Magnitude = random.Next(2) == 0
? Two.Magnitude
: Three.Magnitude;
return;
}
int nBytes = GetByteLength(bitLength);
var b = new byte[nBytes];
int xBits = BitsPerByte*nBytes - bitLength;
byte mask = rndMask[xBits];
for (;;)
{
random.NextBytes(b);
// strip off any excess bits in the MSB
b[0] &= mask;
// ensure the leading bit is 1 (to meet the strength requirement)
b[0] |= (byte) (1 << (7 - xBits));
// ensure the trailing bit is 1 (i.e. must be odd)
b[nBytes - 1] |= 1;
this.Magnitude = MakeMagnitude(b, 0, b.Length);
this.nBits = -1;
this.mQuote = -1L;
if (certainty < 1)
break;
if (CheckProbablePrime(certainty, random))
break;
if (bitLength > 32)
{
for (int rep = 0; rep < 10000; ++rep)
{
int n = 33 + random.Next(bitLength - 2);
this.Magnitude[this.Magnitude.Length - (n >> 5)] ^= (1 << (n & 31));
this.Magnitude[this.Magnitude.Length - 1] ^= ((random.Next() + 1) << 1);
this.mQuote = -1L;
if (CheckProbablePrime(certainty, random))
return;
}
}
}
}
示例6: GetInt64
public static System.Int64 GetInt64(IRandom rand,
System.Int64 min = System.Int64.MinValue,
System.Int64 max = System.Int64.MaxValue)
{
byte[] buffer = new byte[8];
rand.NextBytes(buffer);
System.Int64 result = System.BitConverter.ToInt64(buffer, 0);
if(result < min)
return min;
else if(result >= max)
return max - 1;
else
return result;
}