How to generate an array of 256 distinct numbers

前端 未结 4 570
遇见更好的自我
遇见更好的自我 2021-01-27 03:34

I have this:

#include     
using namespace std;   
int main()
{
    int a[256];
    int b;
    int k;
    for (int i = 0; i < 256; i ++){
             


        
4条回答
  •  臣服心动
    2021-01-27 04:01

    As others mentioned, use std::random_shuffle:

    std::vector my_vec(256); //Reserve space for 256 numbers in advance.
    
    for(int n = 0; n < 256; ++n)
    {
      my_vec.push_back(n);
    }
    
    std::random_shuffle(my_vec.begin(), my_vec.end());
    

提交回复
热议问题