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;
}
从大小为n的数组中随机选出m个整数,要求被选中的概率相同
最新推荐文章于 2021-02-26 23:06:17 发布