Skip to content

Commit

Permalink
Merge pull request #435 from jmarquisbq/filter/FindFeatureClusteringU…
Browse files Browse the repository at this point in the history
…pdates_support

Update RadialDistributionFunction to allow user to set seed value
  • Loading branch information
jmarquisbq committed Apr 3, 2023
2 parents 23492be + eb975a5 commit d1eb67b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 22 additions & 2 deletions Source/SIMPLib/Math/RadialDistributionFunction.cpp
Expand Up @@ -53,6 +53,15 @@ RadialDistributionFunction::~RadialDistributionFunction() = default;
//
// -----------------------------------------------------------------------------
std::vector<float> RadialDistributionFunction::GenerateRandomDistribution(float minDistance, float maxDistance, int numBins, std::array<float, 3>& boxdims, std::array<float, 3>& boxres)
{
return GenerateRandomDistribution(minDistance, maxDistance, numBins, boxdims, boxres, false);
}

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
std::vector<float> RadialDistributionFunction::GenerateRandomDistribution(float minDistance, float maxDistance, int numBins, std::array<float, 3>& boxdims, std::array<float, 3>& boxres,
bool useSeedFromUser, uint64_t userSeedValue)
{
std::vector<float> freq(numBins, 0);
std::vector<float> randomCentroids;
Expand Down Expand Up @@ -82,14 +91,25 @@ std::vector<float> RadialDistributionFunction::GenerateRandomDistribution(float

freq.resize(static_cast<size_t>(current_num_bins + 1));

SIMPL_RANDOMNG_NEW();
std::random_device randomDevice; // Will be used to obtain a seed for the random number engine
std::mt19937_64 generator(randomDevice()); // Standard mersenne_twister_engine seeded with rd()
std::mt19937::result_type seed = userSeedValue;

if(!useSeedFromUser)
{
seed = static_cast<std::mt19937::result_type>(std::chrono::steady_clock::now().time_since_epoch().count());
}

generator.seed(seed);
std::uniform_real_distribution<double> distribution(0.0, 1.0);

randomCentroids.resize(largeNumber * 3);

// Generating all of the random points and storing their coordinates in randomCentroids
for(size_t i = 0; i < largeNumber; i++)
{
featureOwnerIdx = static_cast<size_t>(rg.genrand_res53() * totalpoints);
const auto random = distribution(generator);
featureOwnerIdx = static_cast<size_t>(random * totalpoints);

column = featureOwnerIdx % xpoints;
row = (featureOwnerIdx / xpoints) % ypoints;
Expand Down
4 changes: 4 additions & 0 deletions Source/SIMPLib/Math/RadialDistributionFunction.h
Expand Up @@ -36,6 +36,7 @@
#pragma once

#include <array>
#include <random>
#include <vector>

#include "SIMPLib/SIMPLib.h"
Expand All @@ -61,6 +62,9 @@ class SIMPLib_EXPORT RadialDistributionFunction
*/
static std::vector<float> GenerateRandomDistribution(float minDistance, float maxDistance, int numBins, std::array<float, 3>& boxdims, std::array<float, 3>& boxres);

static std::vector<float> GenerateRandomDistribution(float minDistance, float maxDistance, int numBins, std::array<float, 3>& boxdims, std::array<float, 3>& boxres, bool useSeedFromUser,
uint64_t userSeedValue = std::mt19937::default_seed);

protected:
RadialDistributionFunction();

Expand Down

0 comments on commit d1eb67b

Please sign in to comment.