Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,17 @@ itkConnectedComponentImageFilterTest(int argc, char * argv[])
std::vector<RGBPixelType> colormap;
colormap.resize(numObjects + 1);

using RGBComponentType = RGBPixelType::ComponentType;
constexpr auto maxRGBComponentValue = std::numeric_limits<RGBComponentType>::max();

constexpr std::mt19937::result_type randomSeed{ 1031571 };
std::mt19937 randomNumberEngine(randomSeed);
std::uniform_int_distribution<> randomNumberDistribution(maxRGBComponentValue / 3, maxRGBComponentValue);

for (auto & i : colormap)
{
RGBPixelType px;
std::generate(px.begin(), px.end(), [&randomNumberEngine, &randomNumberDistribution] {
return static_cast<RGBComponentType>(randomNumberDistribution(randomNumberEngine));
std::generate(px.begin(), px.end(), [&randomNumberEngine] {
using RGBComponentType = RGBPixelType::ComponentType;
constexpr uint32_t maxValue{ std::numeric_limits<RGBComponentType>::max() };
constexpr uint32_t minValue{ maxValue / 3 };
return static_cast<RGBComponentType>(randomNumberEngine() % (maxValue - minValue + 1) + minValue);
Comment on lines -133 to +138
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I just compared the result of this "manual" calculation with the result of std::uniform_int_distribution. It appears that both yield the same numbers on old versions of Visual C++ (VS2019). See https://godbolt.org/z/nYsvafs51

#include <iostream>
#include <random>

int main()
{
  constexpr uint32_t maxValue{ std::numeric_limits<unsigned char>::max() };
  constexpr uint32_t minValue{ maxValue / 3 };
  constexpr std::mt19937::result_type randomSeed{ 1031571 };

  int n = 7;
  {
    std::cout << "Using `std::uniform_int_distribution`:    ";
    std::mt19937 randomNumberEngine(randomSeed);
    std::uniform_int_distribution<> randomNumberDistribution(minValue, maxValue);

    for (int i{}; i < n; ++i)
    {
      std::cout << randomNumberDistribution(randomNumberEngine) << ' ';
    }
  }
  std::cout << std::endl;
  {
    std::cout << "Doing the calculation of the pull request: ";
    std::mt19937 randomNumberEngine(randomSeed);

    for (int i{}; i < n; ++i)
    {
      std::cout << (randomNumberEngine() % (maxValue - minValue + 1) + minValue) << ' ';
    }
    std::cout << std::endl;
  }
}

Output from msvc v19.34 VS17.4:

Using `std::uniform_int_distribution`:    119 221 168 148 91 249 122 
Doing the calculation of the pull request: 119 221 168 148 91 249 122 

});

i = px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,16 @@ itkConnectedComponentImageFilterTestRGB(int argc, char * argv[])
RGBPixelType px;
colormap.resize(numObjects + 1);

using RGBComponentType = RGBPixelType::ComponentType;
constexpr auto maxRGBComponentValue = std::numeric_limits<RGBComponentType>::max();

constexpr std::mt19937::result_type randomSeed{ 1031571 };
std::mt19937 randomNumberEngine(randomSeed);
std::uniform_int_distribution<> randomNumberDistribution(maxRGBComponentValue / 3, maxRGBComponentValue);

for (auto & i : colormap)
{
std::generate(px.begin(), px.end(), [&randomNumberEngine, &randomNumberDistribution] {
return static_cast<RGBComponentType>(randomNumberDistribution(randomNumberEngine));
std::generate(px.begin(), px.end(), [&randomNumberEngine] {
using RGBComponentType = RGBPixelType::ComponentType;
constexpr uint32_t maxValue{ std::numeric_limits<RGBComponentType>::max() };
constexpr uint32_t minValue{ maxValue / 3 };
return static_cast<RGBComponentType>(randomNumberEngine() % (maxValue - minValue + 1) + minValue);
});

i = px;
Expand Down
Loading