|
Console Library 4.7.0
A header-only library that makes C++ simple
|
提供随机数生成、随机选择、随机打乱等实用工具。 More...
#include <random>#include <chrono>#include <cstdint>#include <utility>#include <initializer_list>#include <numeric>#include "sfinae.h"#include "csexc.h"Go to the source code of this file.
Namespaces | |
| namespace | console |
| 本库所有组件所在的顶层命名空间。 | |
Functions | |
| std::mt19937 & | console::default_gen () |
| 获取一个全局的、以当前时间戳为种子的 Mersenne Twister 随机数引擎。 | |
| void | console::seed (unsigned int seed) |
| 修改 default_gen 提供的随机数引擎的种子。 | |
| template<class T = int> | |
| T | console::randint (T min=0, T max=32767, std::mt19937 &gen=default_gen()) |
| 生成一个指定范围内的随机整数(均匀分布)。 | |
| template<class T = double> | |
| T | console::uniform (T min=0.0, T max=1.0, std::mt19937 &gen=default_gen()) |
| 生成一个指定范围内的随机浮点数(均匀分布)。 | |
| template<class C> | |
| auto | console::choice (C &c, std::mt19937 &gen=default_gen()) -> decltype(*std::begin(c)) |
| 从容器中随机选择一个元素(左值版本,返回引用)。 | |
| template<class C> | |
| auto | console::choice (C &&c, std::mt19937 &gen=default_gen()) -> std::remove_reference< decltype(*std::begin(c))>::type |
| 从容器中随机选择一个元素(右值版本,返回值)。 | |
| template<class T> | |
| auto | console::choice (std::initializer_list< T > init, std::mt19937 &gen=default_gen()) -> decltype(*std::begin(init)) |
| 从 std::initializer_list 中随机选择一个元素(重载版本)。 | |
| template<class C> | |
| void | console::shuffle (C &&c, std::mt19937 &gen=default_gen()) |
| 随机打乱容器中元素的顺序(Fisher-Yates 洗牌算法)。 | |
| template<class T = double> | |
| std::vector< T > | console::rnorm (size_t n, T mean=0.0, T sd=1.0, std::mt19937 &gen=default_gen()) |
| 生成 n 个服从正态分布(Normal Distribution)的随机数。 | |
| template<class T = double> | |
| std::vector< T > | console::runif (size_t n, T min=0.0, T max=1.0, std::mt19937 &gen=default_gen()) |
| 生成 n 个服从均匀分布(Uniform Distribution)的随机数。 | |
| template<class T = int> | |
| std::vector< T > | console::rbinom (size_t n, T size, double prob, std::mt19937 &gen=default_gen()) |
| 生成 n 个服从二项分布(Binomial Distribution)的随机数。 | |
| template<class T = int> | |
| std::vector< T > | console::rpois (size_t n, double lambda, std::mt19937 &gen=default_gen()) |
| 生成 n 个服从泊松分布(Poisson Distribution)的随机数。 | |
| template<class T = double> | |
| std::vector< T > | console::rexp (size_t n, T rate=1.0, std::mt19937 &gen=default_gen()) |
| 生成 n 个服从指数分布(Exponential Distribution)的随机数。 | |
| template<class T = double> | |
| std::vector< T > | console::rgamma (size_t n, T shape, T rate=1.0, std::mt19937 &gen=default_gen()) |
| 生成 n 个服从伽马分布(Gamma Distribution)的随机数。 | |
| template<class T = double> | |
| std::vector< T > | console::rbeta (size_t n, T shape1, T shape2, std::mt19937 &gen=default_gen()) |
| 生成 n 个服从贝塔分布(Beta Distribution)的随机数。 | |
| template<class T = double> | |
| std::vector< T > | console::rchisq (size_t n, T df, std::mt19937 &gen=default_gen()) |
| 生成 n 个服从卡方分布(Chi-squared Distribution)的随机数。 | |
| template<class T = double> | |
| std::vector< T > | console::rt (size_t n, T df, std::mt19937 &gen=default_gen()) |
| 生成 n 个服从 t 分布(Student's t-distribution)的随机数。 | |
| template<class T = double> | |
| std::vector< T > | console::rf (size_t n, T df1, T df2, std::mt19937 &gen=default_gen()) |
| 生成 n 个服从 F 分布(Fisher–Snedecor F-distribution)的随机数。 | |
| template<class T = double> | |
| std::vector< T > | console::rlnorm (size_t n, T meanlog=0.0, T sdlog=1.0, std::mt19937 &gen=default_gen()) |
| 生成 n 个服从对数正态分布(Lognormal Distribution)的随机数。 | |
| template<class T = double> | |
| std::vector< T > | console::rweibull (size_t n, T shape, T scale=1.0, std::mt19937 &gen=default_gen()) |
| 生成 n 个服从韦布尔分布(Weibull Distribution)的随机数。 | |
| template<class C> | |
| auto | console::sample (C &&c, size_t size, bool replace=false, std::mt19937 &gen=default_gen()) -> std::vector< typename std::iterator_traits< decltype(std::begin(c))>::value_type > |
| 从容器中随机抽取指定数量的元素(有放回或无放回)。 | |
| template<class T> | |
| std::vector< T > | console::sample (std::initializer_list< T > init, size_t size, bool replace=false, std::mt19937 &gen=default_gen()) |
| 从 std::initializer_list 中随机抽样(重载版本)。 | |
提供随机数生成、随机选择、随机打乱等实用工具。