#include <random>

C++ 标准库中的 <random> 头文件提供了一组用于生成随机数的工具,涵盖了从简单的均匀分布到复杂的离散分布,为需要随机数的应用程序提供了广泛的选择。这些工具对于模拟、游戏开发、加密算法等领域非常有用。

<random> 不仅支持生成伪随机数,还支持种子控制、各种概率分布等,使得开发者可以灵活地生成符合特定需求的随机数序列。

// <random> -*- C++ -*-

// Copyright (C) 2007-2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/** @file include/random
 *  This is a Standard C++ Library header.
 */

#ifndef _GLIBCXX_RANDOM
#define _GLIBCXX_RANDOM 1

#pragma GCC system_header

#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else

#include <cmath>
#include <cstdlib>
#include <string>
#include <iosfwd>
#include <limits>
#include <debug/debug.h>
#include <type_traits>

#ifdef _GLIBCXX_USE_C99_STDINT_TR1

#include <cstdint> // For uint_fast32_t, uint_fast64_t, uint_least32_t
#include <bits/random.h>
#include <bits/opt_random.h>
#include <bits/random.tcc>

#endif // _GLIBCXX_USE_C99_STDINT_TR1

#endif // C++11

#endif // _GLIBCXX_RANDOM

<random> 库由以下三个主要组件构成:

  1. 随机数引擎:生成伪随机数的核心,用于控制生成过程的可重复性和随机性。
  2. 随机数分布:控制生成的数值遵循的概率分布类型。
  3. 随机数适配器:允许调整引擎行为,如 discard_block 等适配器。

在 C++ 中,随机数生成器(Random Number Generator, RNG)可以分为两大类:

  • 伪随机数生成器:它们使用确定性算法生成看似随机的数列。这些数列在理论上是可预测的,但通常对于大多数应用来说足够随机。
  • 真随机数生成器:它们基于物理过程(如热噪声、放射性衰变等)生成随机数,但 C++ 标准库不直接提供这类生成器。

常用随机数引擎

引擎描述
std::default_random_engine默认随机数引擎,实现依赖于具体编译器。
std::minstd_rand线性同余引擎,产生均匀的伪随机数序列。
std::mt19937梅森旋转算法,适合通用随机数生成。
std::mt19937_6464 位的梅森旋转算法。
std::ranlux24_base简化的减法进位引擎,用于高质量生成。
std::knuth_bKnuth shuffle 随机数生成器。

常用引擎如 std::mt19937 因为生成速度快且生成质量高,是普遍推荐的随机数生成引擎。生成器还可以使用 seed() 方法指定种子,便于生成可重复的伪随机序列。

随机数分布类型

1、均匀分布

分布描述
std::uniform_int_distribution生成在某个整数范围内的均匀分布。
std::uniform_real_distribution生成在某个浮点数范围内的均匀分布。
std::mt19937 gen(seed);
std::uniform_int_distribution<int> dist(1, 100); // 生成 1 到 100 间的整数
int random_int = dist(gen);

2、正态分布

分布描述
std::normal_distribution标准正态分布,中心对称,常用于模拟自然现象。
std::lognormal_distribution对数正态分布。
std::mt19937 gen(seed);
std::normal_distribution<> dist(0, 1); // 平均值为0,标准差为1
double random_normal = dist(gen);

3、离散分布

分布描述
std::discrete_distribution生成的随机数为一组特定概率的整数。
std::bernoulli_distribution伯努利分布,只生成 true 或 false
std::binomial_distribution二项分布。
std::mt19937 gen(seed);
std::discrete_distribution<int> dist({40, 10, 50}); // 概率为40%、10%、50%
int random_discrete = dist(gen);

语法

使用 <random> 库的基本步骤如下:

  • 包含头文件 <random>
  • 创建一个随机数生成器对象。
  • 使用分布类生成随机数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值