Skip to content
Snippets Groups Projects
Commit aa0c4781 authored by Sandipan Mohanty's avatar Sandipan Mohanty
Browse files

Collect examples for chapter 6

parent 5ab1d0c9
No related branches found
No related tags found
No related merge requests found
Showing with 45 additions and 0 deletions
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
#include <iostream>
#include <map>
#include <random>
auto main() -> int
{
double mn{0.}, var{1.};
std::cout << "Mean : ";
std::cin >> mn;
std::cout << "Variance: ";
std::cin >> var;
std::mt19937_64 engine{ std::random_device{}() };
// Above: First create an object of the type std::random_device.
// random_device objects can be called like functions.
// They give you non-deterministic random numbers, but
// not necessarily of the best mathematical quality.
// We create the object, and immediately call it:
// which is why we have the () after the empty initialiser
// braces {}. This gives us a random unsigned long
// integer, which we then use to seed the Mersenne Twister
// engine.
std::normal_distribution<> dist{mn, std::sqrt(var) };
auto generator = [&]{ return dist(engine); };
std::map<int, unsigned> H;
for (unsigned i = 0; i < 5000000; ++i)
H[static_cast<int>(std::floor(generator()))]++;
for (auto& i : H)
std::cout << i.first << " " << i.second << "\n";
}
#include <array>
#include <iostream>
#include <random>
auto main() -> int
{
std::array freq{ 0.3, 0.2, 0.2, 0.1, 0.1, 0.1 };
std::mt19937_64 engine{ std::random_device{}() };
std::discrete_distribution dist{freq.begin(), freq.end()};
auto gen = [&]{ return dist(engine); };
for (unsigned long i = 0; i < 10'000'000; ++i)
freq[gen()]++;
for (unsigned f : freq)
std::cout << f << std::endl;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment