Ben*_*ley 13
不是特别随机,但考虑到你的限制,你几乎没有选择.
A is the array
S is the size of the array
Generate a random prime number (P), such that P > S
Q = P % S
for i = 1 to S
process A[Q]
Q = (Q + P) % S
Run Code Online (Sandbox Code Playgroud)
使用std::random_shuffle,所以您的代码将是这样的:
std::random_shuffle ( myvector.begin(), myvector.end() ); // in place no extra array
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
Run Code Online (Sandbox Code Playgroud)