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

std::uniform_real_distribution

Defined in header <random>

?

?

template< class RealType = double > class uniform_real_distribution;

?

(since C++11)

产生随机浮点值i,在区间上均匀分布。[a, b),即按概率函数分布:p%28 i a,b%29=。

*。

B-a

...

std::uniform_real_distribution满足…的所有要求RandomNumberDistribution...

模板参数

RealType

-

The result type generated by the generator. The effect is undefined if this is not one of float, double, or long double.

成员类型

Member type

Definition

result_type

RealType

param_type

the type of the parameter set, see RandomNumberDistribution.

成员函数

(constructor)

constructs new distribution (public member function)

reset

resets the internal state of the distribution (public member function)

世代

运算符%28%29在分布%28公共成员函数%29中生成下一个随机数。

特征

AB返回分布参数%28公共成员函数%29

Param获取或设置分布参数对象%28公共成员函数%29

min返回最小潜在生成值%28公共成员函数%29

MAX返回最大潜在生成值%28公共成员函数%29

非会员职能

operator==operator!=

compares two distribution objects (function)

operator<<operator>>

performs stream input and output on pseudo-random number distribution (function template)

注记

一些现有的实现有一个错误,它们可能偶尔返回b,如果RealTypefloat海合会#63176LLVM#18767这是由lwg第2524期

在1到2之间打印10个随机数。

二次

代码语言:javascript
复制
#include <random>
#include <iostream>
 
int main()
{
    std::random_device rd;  //Will be used to obtain a seed for the random number engine
    std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
    std::uniform_real_distribution<> dis(1, 2);
    for (int n = 0; n < 10; ++n) {
        //Use dis to transform the random unsigned int generated by gen into a double in [1, 2)
        std::cout << dis(gen) << ' '; //Each call to dis(gen) generates a new random double
    }
    std::cout << '\n';
}

二次

可能的产出:

二次

代码语言:javascript
复制
1.80829 1.15391 1.18483 1.38969 1.36094 1.0648 1.97798 1.27984 1.68261 1.57326

二次

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com