Build a C++ normal distributed random number generator on Xcode 8.3 -


i'm trying build function returns positive random number taken n(0,1) distribution.

i've looked around on internet , tried use 2 different methods.

this first one, in try set random seed system clock. found on page of cplusplus.com: std::normal_distribution::(constructor)

unsigned long normal01_rand() {          unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();          std::default_random_engine generator (seed);           std::normal_distribution<unsigned long> distribution (0.0, 1.0);           long random_value = distribution(generator);         if (random_value < 0) {             random_value = random_value * (-1);         }         return random_value; } 

and second one. didn't understand how works in detail found answer similar question , thought give try:

unsigned long normal01_rand() {          std::random_device rd;          std::mt19937 e2(rd());          std::normal_distribution<unsigned long> distribution (0.0, 1.0);          long random_value = distribution(e2);         if (random_value < 0) {             random_value = random_value * (-1);         }         return random_value; } 

unfortunately none of them works me. if use first 1 code gets stuck in infinite loop when first try generate random number in code. second 1 instead throws me exception: exc_arithmetic (code=exc_i386_div, subcode=0x0)

i can't understand what's going on , why.

hoping have been clear enough, thank in advance.

it defined as

template< class realtype = double > class normal_distribution 

where realtype use float, double or long double, otherwise undefined.

code should be

std::normal_distribution<double> distribution (0.0, 1.0);  double random_value = distribution(e2); return fabs(random_value); 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -