1. var Solution = function(nums) {
    2. this.nums = nums;
    3. };
    4. Solution.prototype.pick = function(target) {
    5. let ans = 0;
    6. for (let i = 0, cnt = 0; i < this.nums.length; ++i) {
    7. if (this.nums[i] == target) {
    8. ++cnt; // 第 cnt 次遇到 target
    9. if (Math.floor(Math.random() * cnt) === 0) {
    10. ans = i;
    11. }
    12. }
    13. }
    14. return ans;
    15. };

    解决大文件随机抽取同一值的数据