本文整理汇总了C#中MersenneTwister.NextBytes方法的典型用法代码示例。如果您正苦于以下问题:C# MersenneTwister.NextBytes方法的具体用法?C# MersenneTwister.NextBytes怎么用?C# MersenneTwister.NextBytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MersenneTwister
的用法示例。
在下文中一共展示了MersenneTwister.NextBytes方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Measure
private void Measure(SpeedTestDataSourceRow a_row)
{
MersenneTwister r = new MersenneTwister();
{
var ar = r.NextBytes(BYTES_ARRAY_LENGTH);
a_row.BytesSpeed = Measure(ar.Length, () =>
{
a_row.HashFunction.ComputeBytes(ar);
});
}
IFastHash32 hash = a_row.HashFunction as IFastHash32;
if (hash == null)
return;
{
var ar = r.NextBytes(BYTES_ARRAY_LENGTH / SINGLE_ELEMENT_RATIO);
a_row.ByteSpeed = Measure(ar.Length, () =>
{
for (int i = 0; i < ar.Length; i++)
hash.ComputeByteFast(ar[i]);
});
}
{
var ar = r.NextChars(BYTES_ARRAY_LENGTH / sizeof(char));
a_row.CharsSpeed = Measure(ar.Length * sizeof(char), () =>
{
hash.ComputeCharsFast(ar);
});
ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO);
a_row.CharSpeed = Measure(ar.Length * sizeof(char), () =>
{
for (int i = 0; i < ar.Length; i++)
hash.ComputeCharFast(ar[i]);
});
}
{
var ar = r.NextShorts(BYTES_ARRAY_LENGTH / sizeof(short));
a_row.ShortsSpeed = Measure(ar.Length * sizeof(short), () =>
{
hash.ComputeShortsFast(ar);
});
ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO);
a_row.ShortSpeed = Measure(ar.Length * sizeof(short), () =>
{
for (int i = 0; i < ar.Length; i++)
hash.ComputeShortFast(ar[i]);
});
}
{
var ar = r.NextUShorts(BYTES_ARRAY_LENGTH / sizeof(ushort));
a_row.UShortsSpeed = Measure(ar.Length * sizeof(ushort), () =>
{
hash.ComputeUShortsFast(ar);
});
ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO);
a_row.UShortSpeed = Measure(ar.Length * sizeof(ushort), () =>
{
for (int i = 0; i < ar.Length; i++)
hash.ComputeUShortFast(ar[i]);
});
}
{
var ar = r.NextInts(BYTES_ARRAY_LENGTH / sizeof(int));
a_row.IntsSpeed = Measure(ar.Length * sizeof(int), () =>
{
hash.ComputeIntsFast(ar);
});
ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO);
a_row.IntSpeed = Measure(ar.Length * sizeof(int), () =>
{
for (int i = 0; i < ar.Length; i++)
hash.ComputeIntFast(ar[i]);
});
}
{
var ar = r.NextUInts(BYTES_ARRAY_LENGTH / sizeof(uint));
a_row.UIntsSpeed = Measure(ar.Length * sizeof(uint), () =>
//.........这里部分代码省略.........
示例2: AdaptersTest_All
public void AdaptersTest_All()
{
{
IHash md5 = HashFactory.Crypto.CreateMD5();
for(int i = 0; i < 5; i++)
{
var v = new byte[16];
new Random().NextBytes(v);
Assert.IsFalse(!md5.ComputeBytes(v.Take(1).ToArray()).GetBytes().SequenceEqual(md5.ComputeByte(v[0]).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v.Take(2).ToArray()).GetBytes().SequenceEqual(md5.ComputeChar(BitConverter.ToChar(v, 0)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v.Take(2).ToArray()).GetBytes().SequenceEqual(md5.ComputeShort(BitConverter.ToInt16(v, 0)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v.Take(2).ToArray()).GetBytes().SequenceEqual(md5.ComputeUShort(BitConverter.ToUInt16(v, 0)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v.Take(4).ToArray()).GetBytes().SequenceEqual(md5.ComputeInt(BitConverter.ToInt32(v, 0)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v.Take(4).ToArray()).GetBytes().SequenceEqual(md5.ComputeUInt(BitConverter.ToUInt32(v, 0)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v.Take(8).ToArray()).GetBytes().SequenceEqual(md5.ComputeLong(BitConverter.ToInt64(v, 0)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v.Take(8).ToArray()).GetBytes().SequenceEqual(md5.ComputeULong(BitConverter.ToUInt64(v, 0)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v.Take(8).ToArray()).GetBytes().SequenceEqual(md5.ComputeDouble(BitConverter.ToDouble(v, 0)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v.Take(4).ToArray()).GetBytes().SequenceEqual(md5.ComputeFloat(Converters.ConvertBytesToFloat(v, 0)).GetBytes()));
{
string s = BitConverter.ToString(v);
Assert.IsFalse(!md5.ComputeBytes(Converters.ConvertStringToBytes(s)).GetBytes().SequenceEqual(md5.ComputeString(s).GetBytes()));
}
Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeChars(Converters.ConvertBytesToChars(v)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeShorts(Converters.ConvertBytesToShorts(v)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeUShorts(Converters.ConvertBytesToUShorts(v)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeInts(Converters.ConvertBytesToInts(v)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeUInts(Converters.ConvertBytesToUInts(v)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeLongs(Converters.ConvertBytesToLongs(v)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeULongs(Converters.ConvertBytesToULongs(v)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeFloats(Converters.ConvertBytesToFloats(v)).GetBytes()));
Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeDoubles(Converters.ConvertBytesToDoubles(v)).GetBytes()));
}
}
{
IHash murmur2 = HashFactory.Hash32.CreateMurmur2();
for(int i = 0; i < 5; i++)
{
var v = new byte[16];
new Random().NextBytes(v);
Assert.IsFalse(murmur2.ComputeBytes(v.Take(1).ToArray()) != murmur2.ComputeByte(v[0]));
Assert.IsFalse(murmur2.ComputeBytes(v.Take(2).ToArray()) != murmur2.ComputeChar(BitConverter.ToChar(v, 0)));
Assert.IsFalse(murmur2.ComputeBytes(v.Take(2).ToArray()) != murmur2.ComputeShort(BitConverter.ToInt16(v, 0)));
Assert.IsFalse(murmur2.ComputeBytes(v.Take(2).ToArray()) != murmur2.ComputeUShort(BitConverter.ToUInt16(v, 0)));
Assert.IsFalse(murmur2.ComputeBytes(v.Take(4).ToArray()) != murmur2.ComputeInt(BitConverter.ToInt32(v, 0)));
Assert.IsFalse(murmur2.ComputeBytes(v.Take(4).ToArray()) != murmur2.ComputeUInt(BitConverter.ToUInt32(v, 0)));
Assert.IsFalse(murmur2.ComputeBytes(v.Take(8).ToArray()) != murmur2.ComputeLong(BitConverter.ToInt64(v, 0)));
Assert.IsFalse(murmur2.ComputeBytes(v.Take(8).ToArray()) != murmur2.ComputeULong(BitConverter.ToUInt64(v, 0)));
Assert.IsFalse(murmur2.ComputeBytes(v.Take(8).ToArray()) != murmur2.ComputeDouble(BitConverter.ToDouble(v, 0)));
Assert.IsFalse(murmur2.ComputeBytes(v.Take(4).ToArray()) != murmur2.ComputeFloat(Converters.ConvertBytesToFloat(v, 0)));
{
string s = BitConverter.ToString(v);
Assert.IsFalse(murmur2.ComputeBytes(Converters.ConvertStringToBytes(s)) != murmur2.ComputeString(s));
}
Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeChars(Converters.ConvertBytesToChars(v)));
Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeShorts(Converters.ConvertBytesToShorts(v)));
Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeUShorts(Converters.ConvertBytesToUShorts(v)));
Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeInts(Converters.ConvertBytesToInts(v)));
Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeUInts(Converters.ConvertBytesToUInts(v)));
Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeLongs(Converters.ConvertBytesToLongs(v)));
//.........这里部分代码省略.........
示例3: MeasureBytesPerCycle
private void MeasureBytesPerCycle(SpeedTestDataSourceRow a_row)
{
Stopwatch sw = new Stopwatch();
IHash hash = a_row.HashFunction;
MersenneTwister r = new MersenneTwister();
hash.Initialize();
var msg = r.NextBytes(BYTES_ARRAY_LENGTH_CYCLES_PER_SECOND);
RDTSC.rdtsc();
hash.ComputeBytes(msg);
int repeats = 0;
ulong min_delta = UInt64.MaxValue;
for (; ; )
{
HighPriority();
try
{
sw.Start();
ulong before = RDTSC.rdtsc();
hash.ComputeBytes(msg);
ulong after = RDTSC.rdtsc();
sw.Stop();
ulong delta = after - before;
min_delta = Math.Min(min_delta, delta);
}
finally
{
NormalPriority();
}
repeats++;
if (sw.ElapsedMilliseconds > MEASURE_TIME)
if (repeats > MIN_REPEATS)
break;
}
a_row.BytesPerCycle = ((double)min_delta / BYTES_ARRAY_LENGTH_CYCLES_PER_SECOND).ToString("F2");
}
示例4: Save
public void Save()
{
var fs = new FileStream(GetFileName(), FileMode.Create);
using(var sw = new StreamWriter(fs))
{
var random = new MersenneTwister(4563487);
for(int i = 0; i <= m_hash.BlockSize * 3 + 1; i++)
{
byte[] data = random.NextBytes(i);
sw.Write(Convert.ToBase64String(data));
sw.Write("|");
sw.WriteLine(Convert.ToBase64String(m_hash.ComputeBytes(data).GetBytes()));
}
}
}