logo资料库

C++编写生成不重复的随机数代码.pdf

第1页 / 共2页
第2页 / 共2页
资料共2页,全文预览结束
C++编写生成不重复的随机数代码 编写生成不重复的随机数代码 本文给大家汇总介绍了3种c++实现生成不重复的随机数的函数,十分的简单实用,有需要的小伙伴可以参考 下。 C++编写生成不重复的随机数代码 vector getRandom(int total) { srand((int)time(NULL)); std::vector input = *new std::vector(); for (int i = 0; i < total; i++) { input.push_back(i); } vector output = *new vector(); int end = total; for (int i = 0; i < total; i++) { vector::iterator iter = input.begin(); int num = random()%end; iter = iter+num; output.push_back(*iter); input.erase(iter); end--; } return output; } 再来一例: void permutation(int n, int *z_array) { int i, j, k, z; int buffer[N]; /* 初始化数组 */ for (i=0; i #include #include "iostream" #include #define N 53 using namespace std; //print array void display(int *a) { for (int i =0;i
{ int b[N],a[N]; for (int i =0;i
分享到:
收藏