小编use*_*593的帖子

在C++中生成随机非重复数组

我需要在C++中生成随机非重复数组,在这部分代码中我使用srand函数生成随机数,但有些数字是重复的.主要任务是为彩票生成随机数,所以我需要生成数字,直到标记为int golden的黄金数字.

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main()
{
  int golden = 31;
  int i = 0;
  int array[35];

 srand((unsigned)time(0));
    while(i != golden){
        array[i] = (rand()%75)+1;
        cout << array[i] << endl;
        i++;
}
 }
Run Code Online (Sandbox Code Playgroud)

c++ arrays random dynamically-generated

2
推荐指数
1
解决办法
3209
查看次数

标签 统计

arrays ×1

c++ ×1

dynamically-generated ×1

random ×1