Witam, oglądałem ten poradnik: https://www.youtube.com/watch?v=K2nrsQwKmHQ&list=PL5jc9xFGsL8FWtnZBeTqZBbniyw0uHyaH&index=18
Oto mój testowy kod:
#include <iostream>
#include <string>
#include <random>
#include <chrono>
#include <vector>
void printRandom(std::default_random_engine e, std::string introduction) {
std::cout << introduction << ": ";
for (int i = 0; i < 10; ++i)
std::cout << e() << " ";
std::cout.put('\n');
}
int main() {
std::default_random_engine e1;
std::default_random_engine e2;
printRandom(e1, "e1");
printRandom(e2, "e2");
unsigned seed = std::chrono::steady_clock::now().time_since_epoch().count();
std::default_random_engine e3(seed);
printRandom(e3, "e3");
e1.seed();
e1.seed(10);
e2.seed(10);
if (e1 == e2)
std::cout << "e1 and e2 have the same state\n";
std::vector<int> d = { 1,2,3,4,5,6,7,8,9 };
std::shuffle(d.begin(), d.end(), std::default_random_engine(seed));
for (auto it : d)
std::cout << it << " ";
std::cout.put('\n');
std::uniform_int_distribution<int> distr(-2, 5); //Range: <0;5>
std::cout << distr(e3) << std::endl;
std::uniform_real_distribution<double> distrR(0, 5); //Range: <0;5)
std::cout << distrR(e3) << std::endl;
std::poisson_distribution<int> distrP(3.0); //mean
for (int i = 0; i < 100; ++i)
std::cout << distrP(e3) << " ";
std::cout << std::endl;
std::cout << "normal distribution:\n";
std::normal_distribution<double> distrN(10.0, 3.0);
std::vector<int> v(20);
for (int i = 0; i < 800; ++i) {
int num = distrN(e3);
if (num >= 0 && num < 20)
++v[num];
}
for (int i = 0; i < 20; ++i) {
std::cout << i << ": " << std::string(v[i], '*') << std::endl;
}
std::cout << std::endl;
std::cin.get(); std::cin.get();
}
Rozumiem jak działa uniform_int_distribution oraz uniform_real_distribution. Nie rozumiem jednak do końca działania poisson_distribution oraz normal_distribution. Patrzyłem do dokumentacji ale nie wiele mi to mówi :/
Był bym bardzo wdzięczny jak ktoś by w prosty sposób wytłumaczył co robią te szablony oraz ich zastosowanie ( lub dał linka do jakiegoś artykułu ). Z góry dziękuje i pozdrawiam ;)