#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <random>
#include <string>
#include <vector>
namespace lab {
struct Case {
std::string name;
std::string expected_json;
std::string actual_json;
bool passed;
};
std::string esc(const std::string& s) {
std::string out = "\"";
for (unsigned char c : s) {
switch (c) {
case '\"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (c < 0x20) {
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04x", c);
out += buf;
} else {
out += static_cast<char>(c);
}
}
}
out += "\"";
return out;
}
std::string jbool(bool v) { return v ? "true" : "false"; }
void emit(const std::vector<Case>& cases) {
std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
for (std::size_t i = 0; i < cases.size(); ++i) {
if (i) std::cout << ", ";
std::cout << "{\"name\": " << esc(cases[i].name)
<< ", \"expected\": " << cases[i].expected_json
<< ", \"actual\": " << cases[i].actual_json
<< ", \"passed\": " << jbool(cases[i].passed) << "}";
}
std::cout << "]}\n";
}
} // namespace lab
int main() {
std::vector<lab::Case> cases;
// Case 1: mt19937 seed reproducibility & C++11 standard 10000th draw verification
std::mt19937 g1(42), g2(42);
bool same_seq = true;
for (int i = 0; i < 10; ++i) {
if (g1() != g2()) same_seq = false;
}
std::mt19937 def_gen; // default seed 5489u
def_gen.discard(9999);
std::uint_fast32_t ten_thousandth = def_gen();
std::minstd_rand lcg(1);
std::mt19937_64 mt64(1);
std::ranlux24_base rl(1);
std::knuth_b kb(1);
bool other_engines_ok = (lcg() != 0) && (mt64() != 0) && (rl() < (1u << 24)) && (kb() != 0);
bool c1 = same_seq && (ten_thousandth == 4123659995u) && other_engines_ok;
cases.push_back({"mt19937_seed_reproducibility", lab::jbool(true), lab::jbool(c1), c1});
// Case 2: uniform_int_distribution & uniform_real_distribution bounds
std::mt19937 gen_u(2026);
std::uniform_int_distribution<int> dist_i(1, 100);
std::uniform_real_distribution<double> dist_r(0.0, 1.0);
bool in_bounds = true;
for (int i = 0; i < 1000; ++i) {
int vi = dist_i(gen_u);
double vr = dist_r(gen_u);
if (vi < 1 || vi > 100) in_bounds = false;
if (vr < 0.0 || vr >= 1.0) in_bounds = false;
}
cases.push_back({"uniform_int_and_real_distributions", lab::jbool(true), lab::jbool(in_bounds), in_bounds});
// Case 3: normal, lognormal, discrete, bernoulli, binomial distributions
std::mt19937 gen_d(12345);
std::normal_distribution<double> norm(0.0, 1.0);
double sum = 0.0, sq = 0.0;
const int N = 5000;
for (int i = 0; i < N; ++i) {
double x = norm(gen_d);
sum += x;
sq += x * x;
}
double mean = sum / N;
double var = sq / N - mean * mean;
std::lognormal_distribution<double> lnorm(0.0, 0.5);
bool pos_lnorm = (lnorm(gen_d) > 0.0);
std::discrete_distribution<int> disc({10, 20, 70});
int counts[3] = {0, 0, 0};
for (int i = 0; i < 2000; ++i) ++counts[disc(gen_d)];
std::bernoulli_distribution bern(0.5);
int trues = 0;
for (int i = 0; i < 1000; ++i) if (bern(gen_d)) ++trues;
std::binomial_distribution<int> binom(10, 0.5);
bool binom_ok = true;
for (int i = 0; i < 200; ++i) {
int b = binom(gen_d);
if (b < 0 || b > 10) binom_ok = false;
}
bool c3 = (std::fabs(mean) < 0.08) && (std::fabs(var - 1.0) < 0.1) &&
pos_lnorm && (counts[2] > counts[1] && counts[1] > counts[0]) &&
(trues > 400 && trues < 600) && binom_ok;
cases.push_back({"normal_lognormal_and_discrete_distributions", lab::jbool(true), lab::jbool(c3), c3});
// Case 4: discard_block_engine & independent_bits_engine adapters
std::mt19937 ref_gen(777);
std::uint_fast32_t r0 = ref_gen();
std::uint_fast32_t r1 = ref_gen();
(void)ref_gen(); // 3rd discarded by block(3, 2)
std::uint_fast32_t r3 = ref_gen();
std::discard_block_engine<std::mt19937, 3, 2> db(777);
bool db_ok = (db() == r0) && (db() == r1) && (db() == r3);
std::independent_bits_engine<std::mt19937, 16, std::uint32_t> ib(777);
bool ib_ok = true;
for (int i = 0; i < 100; ++i) {
if (ib() >= (1u << 16)) ib_ok = false;
}
bool c4 = db_ok && ib_ok;
cases.push_back({"engine_adapters_discard_and_bits", lab::jbool(true), lab::jbool(c4), c4});
lab::emit(cases);
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。
入口文件:projects/cplusplus-cpp-libs-random-lab/explore-cpp-libs-random.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-libs-random.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2365.zip