Handle random seed generation better for PerlinNoise

Before, the result of `time` was used for the RNG. This change uses
`std::random_device` for a better seed when possible.
This commit is contained in:
Kenneth Moreland 2022-12-01 08:53:54 -07:00
parent 84bc723121
commit aa7b83bb2c
2 changed files with 25 additions and 8 deletions

@ -149,7 +149,26 @@ private:
VTKM_CONT void GeneratePermutations() VTKM_CONT void GeneratePermutations()
{ {
std::mt19937_64 rng; std::mt19937_64 rng;
rng.seed(this->Seed); if (this->Seed != 0)
{
rng.seed(this->Seed);
}
else
{
// If a seed has not been chosen, create a unique seed here. It is done here instead
// of the `PerlinNoise` source constructor for 2 reasons. First, `std::random_device`
// can be slow. If the user wants to specify a seed, it makes no sense to spend
// time generating a random seed only to overwrite it. Second, creating the seed
// here allows subsequent runs of the `PerlinNoise` source to have different random
// results if a seed is not specified.
//
// It is also worth noting that the current time is added to the random number.
// This is because the spec for std::random_device allows it to be deterministic
// if nondeterministic hardware is unavailable and the deterministic numbers can
// be the same for every execution of the program. Adding the current time is
// a fallback for that case.
rng.seed(std::random_device{}() + time(NULL));
}
std::uniform_int_distribution<vtkm::IdComponent> distribution(0, this->TableSize - 1); std::uniform_int_distribution<vtkm::IdComponent> distribution(0, this->TableSize - 1);
vtkm::cont::ArrayHandle<vtkm::Id> perms; vtkm::cont::ArrayHandle<vtkm::Id> perms;
@ -179,11 +198,6 @@ namespace vtkm
namespace source namespace source
{ {
PerlinNoise::PerlinNoise()
: Seed(static_cast<vtkm::IdComponent>(time(NULL)))
{
}
PerlinNoise::PerlinNoise(vtkm::Id3 dims) PerlinNoise::PerlinNoise(vtkm::Id3 dims)
: PerlinNoise() : PerlinNoise()
{ {

@ -29,7 +29,7 @@ namespace source
class VTKM_SOURCE_EXPORT PerlinNoise final : public vtkm::source::Source class VTKM_SOURCE_EXPORT PerlinNoise final : public vtkm::source::Source
{ {
public: public:
VTKM_CONT PerlinNoise(); VTKM_CONT PerlinNoise() = default;
VTKM_CONT ~PerlinNoise() = default; VTKM_CONT ~PerlinNoise() = default;
VTKM_CONT PerlinNoise(const PerlinNoise&) = default; VTKM_CONT PerlinNoise(const PerlinNoise&) = default;
@ -55,6 +55,9 @@ public:
VTKM_CONT vtkm::Vec3f GetOrigin() const { return this->Origin; } VTKM_CONT vtkm::Vec3f GetOrigin() const { return this->Origin; }
VTKM_CONT void SetOrigin(const vtkm::Vec3f& origin) { this->Origin = origin; } VTKM_CONT void SetOrigin(const vtkm::Vec3f& origin) { this->Origin = origin; }
/// \brief The seed used for the pseudorandom number generation of the noise.
///
/// If the seed is set to 0, then a new, unique seed is picked each time `Execute` is run.
VTKM_CONT vtkm::IdComponent GetSeed() const { return this->Seed; } VTKM_CONT vtkm::IdComponent GetSeed() const { return this->Seed; }
VTKM_CONT void SetSeed(vtkm::IdComponent seed) { this->Seed = seed; } VTKM_CONT void SetSeed(vtkm::IdComponent seed) { this->Seed = seed; }
@ -63,7 +66,7 @@ private:
vtkm::Id3 PointDimensions = { 16, 16, 16 }; vtkm::Id3 PointDimensions = { 16, 16, 16 };
vtkm::Vec3f Origin = { 0, 0, 0 }; vtkm::Vec3f Origin = { 0, 0, 0 };
vtkm::IdComponent Seed; vtkm::IdComponent Seed = 0;
}; };
} //namespace source } //namespace source
} //namespace vtkm } //namespace vtkm