【发布时间】:2016-08-15 14:57:24
【问题描述】:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
int main() {
std::vector<short> a(256);
for (short x = 0; x != 256; ++x) {
a[x] = x;
}
for (auto x : a) { std::cout << x << ' '; } std::cout << std::endl;
std::cout << std::endl;
std::srand(11);
std::random_shuffle(a.begin(), a.end());
for (auto x : a) { std::cout << x << ' '; } std::cout << std::endl;
std::cout << std::endl;
for (short x = 0; x != 256; ++x) {
a[x] = x;
}
for (auto x : a) { std::cout << x << ' '; } std::cout << std::endl;
std::cout << std::endl;
std::srand(11);
std::random_shuffle(a.begin(), a.end());
for (auto x : a) { std::cout << x << ' '; } std::cout << std::endl;
}
所以,这是我的代码。我所期望的是,显然,两次都是同样的洗牌。我得到的是,虽然发布之间的洗牌是一致的,但它们是不同的,似乎忽略了 srand!我在这里做错了什么?
【问题讨论】:
标签: c++ random random-seed