CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
randomNumbers.cpp

This example shows how to generate random numbers extracted from a normal distribution

// =================================================================================
// Example code: how to use the NormalRandomNumbers class to generate random numbers
// =================================================================================
#include "Distribution.h"
int main () {
try {
// choose a seed
int seed = 1231;
// choose a range
double xmin = 0.;
double xmax = 10.;
// choose the parameters for the Normal distribution
double mean = 5.;
double sigma = 2.;
// construct the object used to extract random numbers from a Normal distribution
cbl::random::NormalRandomNumbers ran(mean, sigma, seed, xmin, xmax);
// extract the random numbers
std::vector<double> numbers(1000);
for (auto && num : numbers) num = ran();
// compute the distribution of the extracted random numbers
std::vector<double> xx, fx, error;
cbl::distribution(xx, fx, error, numbers, {}, 10);
// show the results
for (size_t i=0; i<xx.size(); ++i)
std::cout << std::setprecision(1) << std::fixed << xx[i] << " " << std::string(fx[i]*100./numbers.size(), '*') << std::endl;
}
catch(cbl::glob::Exception &exc) { std::cerr << exc.what() << std::endl; exit(1); }
return 0;
}
The class Distribution.
int main()
main function to create the logo of the CosmoBolognaLib
Definition: Logo.cpp:41
The class Exception.
Definition: Exception.h:111
const char * what() const noexcept override
the error description
Definition: Exception.h:203
The class NormalRandomNumbers.
void distribution(std::vector< double > &xx, std::vector< double > &fx, std::vector< double > &err, const std::vector< double > FF, const std::vector< double > WW, const int nbin, const bool linear=true, const std::string file_out=par::defaultString, const double fact=1., const double V1=par::defaultDouble, const double V2=par::defaultDouble, const std::string bin_type="Linear", const bool conv=false, const double sigma=0.)
derive and store the number distribution of a given std::vector
Definition: Func.cpp:1654