奇怪的测试行为。我有生成随机值的类。
std::random_device RandomProvider::rd;
std::mt19937 RandomProvider::rnb(RandomProvider::rd());
#define mainDataType unsigned int
mainDataType RandomProvider::GetNextValue(mainDataType upperLimit)
{
static std::uniform_int_distribution<int> uniform_dist(1, upperLimit);
return uniform_dist(rnb);
}
并进行单元测试以测试其行为。
TEST_METHOD(TestRandomNumber)
{
CreateOper(RandomNumber);
int one = 0, two = 0, three = 0, unlim = 0;
const int cycles = 10000;
for (int i = 0; i < cycles; i++)
{
mainDataType res = RandomProvider::GetNextValue(3);
if (res == 1) one++;
if (res == 2) two++;
if (res == 3) three++;
}
double onePerc = one / (double)cycles;
double twoPerc = two / (double)cycles;
double threePerc = three / (double)cycles;
Assert::IsTrue(onePerc > 0.20 && onePerc < 0.40);
Assert::IsTrue(twoPerc > 0.20 && twoPerc < 0.40);
Assert::IsTrue(threePerc > 0.20 && threePerc < 0.40);
}
测试在调试中一直通过,如果我选择它并只运行它。但是当我与其他测试一起运行它时,它总是失败。我将调试输出添加到文本文件并得到虚幻值 onePerc = 0.0556、twoPerc = 0.0474 和threePerc = 0.0526...这是怎么回事?(我正在使用 VS2013 RC)