Last active
July 14, 2021 08:22
-
-
Save alecco/7ed7808095afd4f98b90f808e29723cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g++ -lfmt exponential.cc -o exponential && ./exponential <lambda> | |
// | |
// Using lambda 0.3 | |
// 01: 1% 00189 - *** | |
// 02: 2% 00275 - ***** | |
// 03: 3% 00330 - ****** | |
// 04: 4% 00430 - ******** | |
// 05: 6% 00613 - ************ | |
// 06: 8% 00839 - **************** | |
// 07: 11% 01104 - ********************** | |
// 08: 14% 01467 - ***************************** | |
// 09: 20% 02038 - **************************************** | |
// 10: 27% 02715 - ****************************************************** | |
// useful samples 70% | |
// | |
#include <cstdio> | |
#include <random> | |
#include <fmt/core.h> | |
#include <fmt/ranges.h> | |
size_t samples = 10000; | |
size_t min = 1; | |
size_t max = 10; | |
float lambda = 0.3; // default lambda | |
int main(int argc, char *argv[]) { | |
if (argc == 2) { | |
lambda = atof(argv[1]); | |
} | |
fmt::print("Using lambda {}\n", lambda); | |
std::vector<size_t> b(10); | |
std::default_random_engine re{std::random_device{}()}; | |
std::exponential_distribution<> dist(lambda); | |
size_t n = 0; | |
for(int s = 0; s < samples; ++n) { | |
auto val = int(dist(re)); | |
if (val >= min && val <= max) { // val in [1;10] | |
++b[max - val]; | |
++s; | |
} | |
} | |
for(size_t i = 0; i < b.size(); i++) { | |
fmt::print(" {:02}: {:2}% {:05} - {}\n", | |
i + 1, b[i]*100/samples, b[i], std::string(b[i]/50, '*')); | |
} | |
fmt::print("useful samples {:02}%\n", samples * 100/n); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment