C++ 标准库 —— random

#include 

引擎(engine)与分布(distribution)

  • 引擎

    std::default_random_engine dre;

    engine 可作为 std::shuffle() 的第三个参数进行传递;

  • 分布

    std::uniform_int_distribution di(10, 20);
                                        // 包含端点
    std::uniform_real_distribution dr(10, 20);
                                        // 不包含右端点
std::default_random_engine dre;
std::uniform_int_distribution di(10, 20);
for (int i = 0; i < 20; ++i)
    cout << di(dre) << " ";
cout << endl;

std::uniform_real_distribution dr(10, 20);
for (int i = 0; i < 20; ++i)
    cout << dr(dre) << " ";
cout << endl;

std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::shuffle(v.begin(), v.end(), dre);
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;

你可能感兴趣的:(C/C++)