从大小为n的数组中随机选出m个整数,要求被选中的概率相同

int* selectMElements(int* data, int n, int m) { int* res = new int[m]; for (int i = 0; i < m; i++) { res[i] = data[i]; } for (int i = m; i < n; i++) { int k = random(0, i); if (k < m) { res[k] = data[i]; } } return res; }


    推荐阅读