C++11 random number generators are consistent across platforms, but the distribution classes are not: http://stackoverflow.com/a/13589262

This commit is contained in:
David Williams 2015-02-10 23:35:17 +01:00
parent f87966686d
commit 7e19706681
2 changed files with 8 additions and 14 deletions

View File

@ -67,7 +67,6 @@ VolumeType* createAndFillVolumeWithNoise(int32_t iVolumeSideLength, typename Vol
// Set up a random number generator // Set up a random number generator
std::mt19937 rng; std::mt19937 rng;
std::uniform_int_distribution<int32_t> dist(minValue, maxValue);
//Fill the volume with data //Fill the volume with data
for (int32_t z = 0; z < iVolumeSideLength; z++) for (int32_t z = 0; z < iVolumeSideLength; z++)
@ -84,7 +83,9 @@ VolumeType* createAndFillVolumeWithNoise(int32_t iVolumeSideLength, typename Vol
else else
{ {
// Otherwise we write random voxel values between zero and the requested maximum // Otherwise we write random voxel values between zero and the requested maximum
typename VolumeType::VoxelType voxelValue = static_cast<typename VolumeType::VoxelType>(dist(rng)); // We can't use std distributions because they vary between platforms (breaking tests).
int voxelValue = (rng() % (maxValue - minValue + 1)) + minValue; // +1 for inclusive bounds
volData->setVoxelAt(x, y, z, static_cast<typename VolumeType::VoxelType>(voxelValue)); volData->setVoxelAt(x, y, z, static_cast<typename VolumeType::VoxelType>(voxelValue));
} }
} }

View File

@ -128,15 +128,6 @@ VolumeType* createAndFillVolume(void)
return volData; return volData;
} }
// From http://stackoverflow.com/a/5289624
float randomFloat(float a, float b)
{
float random = ((float)rand()) / (float)RAND_MAX;
float diff = b - a;
float r = random * diff;
return a + r;
}
template <typename VolumeType> template <typename VolumeType>
VolumeType* createAndFillVolumeWithNoise(int32_t iVolumeSideLength, float minValue, float maxValue) VolumeType* createAndFillVolumeWithNoise(int32_t iVolumeSideLength, float minValue, float maxValue)
{ {
@ -147,7 +138,6 @@ VolumeType* createAndFillVolumeWithNoise(int32_t iVolumeSideLength, float minVal
// Set up a random number generator // Set up a random number generator
std::mt19937 rng; std::mt19937 rng;
std::uniform_real_distribution<float> dist(minValue, maxValue);
// Fill // Fill
for (int32_t z = 0; z < iVolumeSideLength; z++) for (int32_t z = 0; z < iVolumeSideLength; z++)
@ -156,7 +146,10 @@ VolumeType* createAndFillVolumeWithNoise(int32_t iVolumeSideLength, float minVal
{ {
for (int32_t x = 0; x < iVolumeSideLength; x++) for (int32_t x = 0; x < iVolumeSideLength; x++)
{ {
float voxelValue = dist(rng); // We can't use std distributions because they vary between platforms (breaking tests)
float voxelValue = static_cast<float>(rng()) / static_cast<float>(std::numeric_limits<int32_t>::max()); // Float in range 0.0 to 1.0
voxelValue = voxelValue * (maxValue - minValue) + minValue; // Float in range minValue to maxValue
volData->setVoxelAt(x, y, z, voxelValue); volData->setVoxelAt(x, y, z, voxelValue);
} }
} }
@ -233,7 +226,7 @@ void TestSurfaceExtractor::testNoiseVolumePerformance()
auto noiseVol = createAndFillVolumeWithNoise< PagedVolume<float> >(128, -1.0f, 1.0f); auto noiseVol = createAndFillVolumeWithNoise< PagedVolume<float> >(128, -1.0f, 1.0f);
Mesh< MarchingCubesVertex< float >, uint16_t > noiseMesh; Mesh< MarchingCubesVertex< float >, uint16_t > noiseMesh;
QBENCHMARK{ extractMarchingCubesMeshCustom(noiseVol, Region(32, 32, 32, 63, 63, 63), &noiseMesh); } QBENCHMARK{ extractMarchingCubesMeshCustom(noiseVol, Region(32, 32, 32, 63, 63, 63), &noiseMesh); }
QCOMPARE(noiseMesh.getNoOfVertices(), uint16_t(48704)); QCOMPARE(noiseMesh.getNoOfVertices(), uint16_t(36755));
} }
QTEST_MAIN(TestSurfaceExtractor) QTEST_MAIN(TestSurfaceExtractor)