我正在研究一个在parallel.for循环中需要随机数的C#项目.为此,我使用Jon Skeet的MiscUtil中的StaticRandom类.
为了测试,我希望能够重现我的结果.因此,我尝试在每次测试运行时为底层System.Random播种以获得相同的序列.然而,即使种子我每次都得到不同的结果.在常规for循环中,每次输出相同的序列.下面有代码重现我的问题(您必须使用机器的输出更新预期的顺序).
有没有办法播种Random,所以我可以在parallel.for循环中获得可重现的序列?
[TestMethod]
public void MultiThreadedSeededRandom()
{
var actual = new ConcurrentBag<int>();
Parallel.For(0, 10, i =>
{
actual.Add(StaticRandom.Next(1000));
});
WriteActualToOutput(actual);
var expected = new int[] { 743, 51, 847, 682, 368, 959, 245, 849, 192, 440, };
Assert.IsTrue(AreEqual(expected, actual.ToArray()));
}
public static bool AreEqual<T>(T[] expected, T[] actual)
{
if (expected.Length != actual.Length)
return false;
for (int i = 0; i < expected.Length; i++)
{
if (!expected[i].Equals(actual[i]))
return false;
}
return true;
}
private static void WriteActualToOutput(ConcurrentBag<int> acual)
{ …Run Code Online (Sandbox Code Playgroud)