C++中生成[0, N-1]随机序列

输入 随机序列长度,返回 随机序列

int * randpermC(int N)
{
    int *arr = (int*)malloc(N * sizeof(int));
    int *arr2 = (int*)malloc(N * sizeof(int));
    int count = 0;
    memset(arr, 0, N * sizeof(int));
    srand(time(NULL));
    while (count<N)
    {
        int val = rand() % N;
        if (!arr[val])
        {
            arr[val] = 1;
            arr2[count] = val;
            ++count;
        }
    }
    return arr2;
}

arr用于标记是否使用,arr2用于记录返回的随机序列

 

posted @ 2020-06-26 03:01  sbj123456789  阅读(542)  评论(0编辑  收藏  举报