简体   繁体   English

从JS中大小为n的数组中随机选择m个整数

[英]Pick m integers randomly from an array of size n in JS

Problem (from Cracking the Coding Interview ): Write a method to randomly generate a set of m integers from an array of size n. 问题 (来自破解编码面试 ):编写一种方法,从大小为n的数组中随机生成一组m个整数。 Each element must have equal probability of being chosen. 每个元素必须具有相等的被选择概率。

I'm implementing my answer in JS. 我正在用JS实现我的答案。 For the recursive function, the code sometimes returns undefined as one of the elements in the array. 对于递归函数,代码有时返回undefined作为数组中的元素之一。

My JS Code 我的JS代码

var pickMRecursively = function(A, m, i) {
    if (i === undefined) return pickMRecursively(A, m, A.length);
    if (i+1 === m) return A.slice(0, m);

    if (i + m > m) {
        var subset = pickMRecursively(A, m, i-1);
        var k = rand(0, i);
        if (k < m) subset[k] = A[i];

        return subset;
    }

    return null;
};

Given Java Solution 给定的Java解决方案

int[] pickMRecursively(int[] original, int m,int i) {
    if (i +1==m){// Basecase
     /* return first m elements of original */
    } elseif(i+m>m){
        int[] subset = pickMRecursively(original, m, i - 1); 
        int k = random value between 0 and i, inclusive
        if(k<m){
            subset[k] = original[i]j
        }
        return subset;
    }
    return null;
}

I hate these questions because sometime they're often deliberately vague - I'd ask "What type of data is in the array?". 我讨厌这些问题,因为有时它们通常是故意含糊的-我会问“数组中的数据类型是什么?”。 But if this is actually a question about randomly re-ordering an array, then in JavaScript, given that arr is an array of numbers, of which some/all might not be integers... 但是,如果这实际上是关于随机重新排列数组的问题,那么在JavaScript中,假设arr是一个数字数组,其中某些/全部可能不是整数...

function generateM(arr) {
  var hold = [];
  var m = [];
  var n = arr.length;
  var grab;

  // clone arr >> hold
  while(n--) {
    hold[n] = arr[n];
  }
  n = hold.length;

  // select randomly from hold
  while(n--) {
    grab = hold.splice(Math.floor(Math.random()*n),1)[0];
    // ensure integers
    m.push(Math.round(grab));
  }

  return m;
}

The array arr is cloned here to cover scoping issues and to produce a fresh collection, not reorder an existing one. 在此处克隆了数组arr ,以涵盖范围界定问题并产生新的集合,而不是对现有集合进行重新排序。

ADDIT : Alternatively, if this is just asking for a set of m.length random integers generated from an array of n.length , then it doesn't matter what the content of the array actually is and the range of possible (randomly generated) values will be (could be?) 0 - n.length , so... ADDIT :或者,如果这只是要求从n.length数组生成的一组m.length随机整数,则该数组的实际内容和可能的范围(随机生成)都无关紧要值将是(可能是?) 0 - n.length ,所以...

function generateM(arr, M) {
  var aLen = arr.length;
  var m = [];

  do {
    m.push(Math.round(Math.random() * aLen));
  } while(M--);

  return m;
}

...but that seems like a stupid, pointless challenge. ...但这似乎是一个愚蠢,毫无意义的挑战。 The data in the 'array of size n' is quite important here it seems to me. 在我看来,“大小为n的数组”中的数据非常重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM