如何随机化一组数字

时间:2017-02-07 22:50:06

标签: c++

让我们说我想让用户输入随机化。 就像用户输入“1234567890”

一样

输出结果如“5684319027” 或任何随机数,因为数字是随机的。

那么我如何随机化用户输入?

1 个答案:

答案 0 :(得分:0)

您可以使用std::shuffle随机化几乎任何有序结构的元素顺序。您可以在std::string上使用此功能,这将重新排列单个字符。以下是一个使用示例:

// at the top of your file
#include <algorithm>

// in your main code
std::string str; // define a string variable
std::cin >> str; // get user input into the string

// shuffle the string with a default random engine
std::shuffle(str.begin(), str.end(), std::default_random_engine(std::random_device{}()));

// use the now-shuffled string...
相关问题