Code Monkey home page Code Monkey logo

Comments (13)

lessfish avatar lessfish commented on May 26, 2024

@dayuoba 啥意思?

from underscore-analysis.

ecmadao avatar ecmadao commented on May 26, 2024

@dayuoba
同求,没有明白什么意思。。
我能想到的是,在循环过程中,随着index的增大,~~(Math.random() * (index + 1))能够选到首元素的概率会降低。但其实因为被random基数的增加,之前的每个元素被随机选到的可能性是一样的(不考虑Math.random伪随机的情况下)

for (var index = 0, rand; index < length; index++) {
    rand = ~~(Math.random() * (index + 1));
   // .....
}

from underscore-analysis.

lessfish avatar lessfish commented on May 26, 2024

@ecmadao 不知道你说的没明白是没明白哪块?

from underscore-analysis.

ecmadao avatar ecmadao commented on May 26, 2024

@hanzichi
就是 @dayuoba 所说的Fisher–Yates Shuffle 算法,首元素被洗掉的概率会越来越低

from underscore-analysis.

lessfish avatar lessfish commented on May 26, 2024

@ecmadao 首元素被洗掉的概率确实越来越低了,但是首元素一直在变的呀,而且我们关心的是每个元素随机到每个位置的概率是否一致。

from underscore-analysis.

ecmadao avatar ecmadao commented on May 26, 2024

@hanzichi
嗯那其实我们理解的一致,每个元素分配到的概率是总相同的。其实之前我都不知道有Fisher–Yates Shuffle,THX

from underscore-analysis.

zhoucumt avatar zhoucumt commented on May 26, 2024

请教一下,_.intersection这个求数组交集的方法,为什么if (j === argsLength)的时候,就可以result.push(item);,这个判断条件没想明白

from underscore-analysis.

lessfish avatar lessfish commented on May 26, 2024

@zhoucumt
遍历其他数组,如果其他数组中不存在 item 这个元素,那么就会 break 掉,这时 j 肯定小于 argsLength;而如果每个数组都存在 item 元素,则 for 循环可以执行到最后,for 循环结束时 j === argsLength,所以 j === argsLength 满足则说明每个数组都存在 item 元素。

其实我觉得源码注释说的好像蛮清楚了 ...

// Produce an array that contains every item shared between all the
// passed-in arrays.
// 寻找几个数组**有的元素
// 将这些每个数组中都有的元素存入另一个数组中返回
// _.intersection(*arrays)
// _.intersection([1, 2, 3, 1], [101, 2, 1, 10, 1], [2, 1, 1])
// => [1, 2]
// 注意:返回的结果数组是去重的
_.intersection = function(array) {
  // 结果数组
  var result = [];

  // 传入的参数(数组)个数
  var argsLength = arguments.length;

   // 遍历第一个数组的元素
  for (var i = 0, length = getLength(array); i < length; i++) {
    var item = array[i];

    // 如果 result[] 中已经有 item 元素了,continue
    // 即 array 中出现了相同的元素
    // 返回的 result[] 其实是个 "集合"(是去重的)
    if (_.contains(result, item)) continue;

    // 判断其他参数数组中是否都有 item 这个元素
    for (var j = 1; j < argsLength; j++) {
      if (!_.contains(arguments[j], item)) break;
    }

    // 遍历其他参数数组完毕
    // j === argsLength 说明其他参数数组中都有 item 元素
    // 则将其放入 result[] 中
    if (j === argsLength) result.push(item);
  }

  return result;
};

另外,如果跟本主题无关的问题,希望能开个新的 issue ... 第一反应以为是关于数组乱序的问题 ... thanks

from underscore-analysis.

lessfish avatar lessfish commented on May 26, 2024

感觉 GitHub 怎么自动把 <> 取消掉了 ... 囧

from underscore-analysis.

Mess1ah avatar Mess1ah commented on May 26, 2024

请问 _.sample 里面有个参数n 代表 返回n个元素 源码中return _.shuffle(obj).slice(0, Math.max(0,n)) 这里为什么要用Math.max(0,n) ? underscore中有好多地方都用了Math.max(0,n),为什么不能直接写n

from underscore-analysis.

Nanchenk avatar Nanchenk commented on May 26, 2024

如果没记错的话,不是Math.random影像了结果,而是sort的实现,在20以内是插入排序,大于20是快排,而且不同浏览器的实现也不一样,ff好像是合并排序解决长数组的,恩,maybe吧

from underscore-analysis.

huyansheng3 avatar huyansheng3 commented on May 26, 2024

Fisher–Yates Shuffle 这个算法从尾部开始,将index 的内容和index 之前的数组中随机的一个交换,index 一直往前移动,但是到快要终止的时候,只能是 index =1 的索引和 index =0的索引交换,这样的新的数组,首部的数字相对固定。 在一些场景下,有一定缺陷。

第一种的方式则不会存在这个问题。

from underscore-analysis.

zxalfred avatar zxalfred commented on May 26, 2024

@huyansheng3

从循环开始时起,首部数字即有被后面数字交换的概率,并不是当循环往前移动到相应位置时才能发生交换。所以不存在首部相对固定的问题。

from underscore-analysis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.