首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::random_device

Defined in header <random>

class random_device;

(since C++11)

std::random_device是一个均匀分布的整数随机数生成器,它产生非确定性随机数。

std::random_device如果不确定源%28例如,则可以根据实现定义的伪随机数引擎来实现。硬件设备%29不能用于实现。在这种情况下,每个std::random_device对象可以生成相同的数字序列。

成员类型

Member type

Definition

result_type

unsigned int

成员函数

建设

*。

%28构造函数%29构造引擎%28公共成员函数%29

运算符=%28已删除%29赋值运算符被删除%28公共成员函数%29

世代

运算符%28%29提升引擎%27s状态并返回生成值%28公共成员函数%29

特征

熵得到了非确定性随机数发生器%28公共成员函数%29的熵估计。

敏静态获取输出范围%28公共静态成员函数%29中的最小可能值。

马克斯静态获取输出范围%28公共静态成员函数%29中的最大可能值。

二次

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <map>
#include <random>
 
int main()
{
    std::random_device rd;
    std::map<int, int> hist;
    std::uniform_int_distribution<int> dist(0, 9);
    for (int n = 0; n < 20000; ++n) {
        ++hist[dist(rd)]; // note: demo only: the performance of many 
                          // implementations of random_device degrades sharply
                          // once the entropy pool is exhausted. For practical use
                          // random_device is generally only used to seed 
                          // a PRNG such as mt19937
    }
    for (auto p : hist) {
        std::cout << p.first << " : " << std::string(p.second/100, '*') << '\n';
    }
}

二次

可能的产出:

二次

代码语言:javascript
复制
0 : ********************
1 : *******************
2 : ********************
3 : ********************
4 : ********************
5 : *******************
6 : ********************
7 : ********************
8 : *******************
9 : ********************

二次

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券