Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
random.h File Reference

提供随机数生成、随机选择、随机打乱等实用工具。 More...

#include <chrono>
#include <cstdint>
#include <initializer_list>
#include <numeric>
#include <random>
#include <utility>
#include "../core/csexc.h"
#include "../core/sfinae.h"

Go to the source code of this file.

Namespaces

namespace  console
 本库所有组件所在的顶层命名空间。

Functions

std::mt19937 & console::thread_rng ()
 获取一个全局的、以当前时间戳为种子的 Mersenne Twister 随机数引擎。
template<class T>
void console::seed (T seed)
 修改 thread_rng 提供的随机数引擎的种子。
template<class T = int>
T console::randint (T min=0, T max=32767, std::mt19937 &gen=thread_rng())
 生成一个指定范围内的随机整数(均匀分布)。
template<class T = double>
T console::uniform (T min=0.0, T max=1.0, std::mt19937 &gen=thread_rng())
 生成一个指定范围内的随机浮点数(均匀分布)。
bool console::randbool (double p=0.5, std::mt19937 &gen=thread_rng())
 生成一个服从伯努利分布(Bernoulli Distribution)的随机布尔值。
template<class C>
auto console::choice (C &c, std::mt19937 &gen=thread_rng()) -> decltype(*std::begin(c))
 从容器中随机选择一个元素(左值版本,返回引用)。
template<class C>
auto console::choice (const C &c, std::mt19937 &gen=thread_rng()) -> decltype(*std::begin(c))
 从容器中随机选择一个元素(常量左值版本,返回 const 引用)。
template<class C>
auto console::choice (C &&c, std::mt19937 &gen=thread_rng()) -> decltype(*std::begin(c))
 从容器中随机选择一个元素(右值版本,返回值)。
template<class T>
T console::choice (std::initializer_list< T > init, std::mt19937 &gen=thread_rng())
 从 std::initializer_list 中随机选择一个元素(重载版本)。
template<class C>
void console::shuffle (C &&c, std::mt19937 &gen=thread_rng())
 随机打乱容器中元素的顺序(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=thread_rng())
 生成 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=thread_rng())
 生成 n 个服从均匀分布(Uniform Distribution)的随机数。
template<class T = int>
std::vector< T > console::rbinom (size_t n, T size, double prob, std::mt19937 &gen=thread_rng())
 生成 n 个服从二项分布(Binomial Distribution)的随机数。
template<class T = int>
std::vector< T > console::rpois (size_t n, double lambda, std::mt19937 &gen=thread_rng())
 生成 n 个服从泊松分布(Poisson Distribution)的随机数。
template<class T = double>
std::vector< T > console::rexp (size_t n, T rate=1.0, std::mt19937 &gen=thread_rng())
 生成 n 个服从指数分布(Exponential Distribution)的随机数。
template<class T = double>
std::vector< T > console::rgamma (size_t n, T shape, T rate=1.0, std::mt19937 &gen=thread_rng())
 生成 n 个服从伽马分布(Gamma Distribution)的随机数。
template<class T = double>
std::vector< T > console::rbeta (size_t n, T shape1, T shape2, std::mt19937 &gen=thread_rng())
 生成 n 个服从贝塔分布(Beta Distribution)的随机数。
template<class T = double>
std::vector< T > console::rchisq (size_t n, T df, std::mt19937 &gen=thread_rng())
 生成 n 个服从卡方分布(Chi-squared Distribution)的随机数。
template<class T = double>
std::vector< T > console::rt (size_t n, T df, std::mt19937 &gen=thread_rng())
 生成 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=thread_rng())
 生成 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=thread_rng())
 生成 n 个服从对数正态分布(Lognormal Distribution)的随机数。
template<class T = double>
std::vector< T > console::rweibull (size_t n, T shape, T scale=1.0, std::mt19937 &gen=thread_rng())
 生成 n 个服从韦布尔分布(Weibull Distribution)的随机数。
template<class C>
std::vector< typename C::value_type > console::sample (const C &c, size_t size, bool replace=false, std::mt19937 &gen=thread_rng())
 从容器中随机抽取指定数量的元素(有放回或无放回)。
template<class T>
std::vector< T > console::sample (std::initializer_list< T > init, size_t size, bool replace=false, std::mt19937 &gen=thread_rng())
 从 std::initializer_list 中随机抽样(重载版本)。

Detailed Description

提供随机数生成、随机选择、随机打乱等实用工具。

Author
MrXie1109
Date
2026