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

use*_*593 2 c++ arrays random dynamically-generated

我需要在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)

NPE*_*NPE 7

一种策略是使用1到75之间的数字填充数组,然后使用std::random_shuffle()它.然后,您可以从数组中读取数字,直到您达到黄金数字.