gpt4 book ai didi

c++ - 随机顺序的连续数字

转载 作者:行者123 更新时间:2023-11-30 01:54:24 24 4
gpt4 key购买 nike

如何生成一个包含一组连续数字但顺序随机的数组?例如,连续的数字是从 8 到 100(没有重复),我想要的是一个包含所有这些数字但顺序是随机的数组。

最佳答案

我想 8 和 100 都包含在该范围内。

#include <algorithm>
#include <numeric>
#include <iterator>

int main()
{
const size_t N = 93;
int a[N];

std::iota( std::begin( a ), std::end( a ), 8 );
std::random_shuffle( std::begin( a ), std::end( a ) );
}

如果 8 是您帖子中的拼写错误,而您的意思是 0,那么代码将如下所示

   const size_t N = 101;
int a[N];

std::iota( std::begin( a ), std::end( a ), 0 );

关于c++ - 随机顺序的连续数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22049880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com