不同实现之间的随机输出不同 [英] Random output different between implementations
问题描述
我已经使用libstdc ++,libc ++和dinkumware尝试过该程序:
I've tried this program with libstdc++, libc++ and dinkumware:
#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <functional>
#include <limits>
int main()
{
std::vector<int> v(10);
std::mt19937 rand{0};
std::uniform_int_distribution<> dist(
1, 10
);
std::generate_n(v.begin(), v.size(),
std::bind(dist, rand));
for (auto i : v)
std::cout << i << " ";
}
输出分别为:
6 6 8 9 7 9 6 9 5 7
6 1 4 4 8 10 4 6 3 5
5 10 4 1 4 10 8 4 8 4
每次运行的输出都是一致的,但是正如您所看到的,它们是不同的.解释吗?
The output is consistent for each run but as you can see, they're different. Explain?
推荐答案
uniform_int_distribution<>
不需要不需要. [rand.dist.general]指定:
There is no required implementation for uniform_int_distribution<>
. [rand.dist.general] specifies that:
产生每个指定分布的算法是实现定义的.
The algorithms for producing each of the specified distributions are implementation-defined.
[rand.dist.uni.int]所陈述的全部是:
All that [rand.dist.uni.int] states is:
uniform_int_distribution随机数分布产生随机整数
i
,a <= i <= b
,分布 根据常数离散概率函数P(i | a, b) = 1/(b − a + 1)
.
A uniform_int_distribution random number distribution produces random integers
i
,a <= i <= b
, distributed according to the constant discrete probability functionP(i | a, b) = 1/(b − a + 1)
.
每个实现都可以按照自己的意愿随意实现此分发.您所看到的显然是三种不同的实现.
Each implementation is free to achieve this distribution how it wishes. What you are seeing is apparently three different implementations.
这篇关于不同实现之间的随机输出不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!