Skip to content

Commit

Permalink
Update RadialDistributionFunction to allow the user to set the seed v…
Browse files Browse the repository at this point in the history
…alue for the random number generation (#436)

Co-authored-by: Jessica Marquis <jessica.marquis@bluequartz.net>
  • Loading branch information
imikejackson and jmarquisbq committed Apr 3, 2023
1 parent 23492be commit 44b7bca
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 44b7bca

Please sign in to comment.