Given a stream of elements too large to store in memory, pick a random element from the stream with uniform probability.

To solve the problem which n size is unknown, Reservior Sampling is a perfect algorithm to use:

Reservoir sampling algorithm can be used for randomly choosing a sample from a stream of n items, where n is unknow.

Here we still need to prove that

Consider the (i)th item, with its compatibility probability of 1/i. The probability I will be choose the i at the time n > i can be demonstrated by a simple formula

i/i: Probability the ith item will be selected;

(1 - i/i+1): Probability the i+1th item will NOT be selected;

(1 - i/i+2): Probability the i+2th item will NOT be selected;

(1 - 1 / n): Probability the nth item will NOT be selected;

In the end, the probability of ith item will be selected at given n, which n > i is 1/n.

Let’s attempt to solve using loop invariants. On the ith iteration of our loop to pick a random element, let’s assume we already picked an element uniformly from [0, i - 1]. In order to maintain the loop invariant, we would need to pick the ith element as the new random element at 1 / (i + 1) chance. For the base case where i = 0, let’s say the random element is the first one.

function Reservoir_Sampling (ary) {
let selected;
const size = ary.length; for (let i = 0; i < size; i++) {
if (Math.floor(Math.random() * size) === 1) {
selected = ary[i];
break;
}
} return selected;
}

[Algorithm] Reservoir Sampling的更多相关文章

  1. 【算法34】蓄水池抽样算法 (Reservoir Sampling Algorithm)

    蓄水池抽样算法简介 蓄水池抽样算法随机算法的一种,用来从 N 个样本中随机选择 K 个样本,其中 N 非常大(以至于 N 个样本不能同时放入内存)或者 N 是一个未知数.其时间复杂度为 O(N),包含 ...

  2. 算法系列:Reservoir Sampling

    copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  3. 蓄水池采样算法(Reservoir Sampling)

    蓄水池采样算法 问题描述分析 采样问题经常会被遇到,比如: 从 100000 份调查报告中抽取 1000 份进行统计. 从一本很厚的电话簿中抽取 1000 人进行姓氏统计. 从 Google 搜索 & ...

  4. Reservoir Sampling - 蓄水池抽样

    问题起源于编程珠玑Column 12中的题目10,其描述如下: How could you select one of n objects at random, where you see the o ...

  5. 水塘抽样(Reservoir Sampling)问题

    水塘抽样是一系列的随机算法,其目的在于从包含n个项目的集合S中选取k个样本,其中n为一很大或未知的数量,尤其适用于不能把所有n个项目都存放到主内存的情况. 在高德纳的计算机程序设计艺术中,有如下问题: ...

  6. Spark MLlib之水塘抽样算法(Reservoir Sampling)

    1.理解 问题定义可以简化如下:在不知道文件总行数的情况下,如何从文件中随机的抽取一行? 首先想到的是我们做过类似的题目吗?当然,在知道文件行数的情况下,我们可以很容易的用C运行库的rand函数随机的 ...

  7. Reservoir Sampling - 蓄水池抽样问题

    问题起源于编程珠玑Column 12中的题目10,其描述如下: How could you select one of n objects at random, where you see the o ...

  8. 蓄水池抽样算法 Reservoir Sampling

    2018-03-05 14:06:40 问题描述:给出一个数据流,这个数据流的长度很大或者未知.并且对该数据流中数据只能访问一次.请写出一个随机选择算法,使得数据流中所有数据被选中的概率相等. 问题求 ...

  9. 随机抽样问题(蓄水池问题Reservoir Sampling)

    转自:孤影醉残阳 http://hi.baidu.com/siyupy/item/e4bb218fedf4a0864414cfad 随机抽样问题(蓄水池问题Reservoir Sampling) 随即 ...

随机推荐

  1. vbs学习笔记1——判断文件和文件夹是否存在

    首先分享一个“VBS脚本常用经典代码收集”,这里面关于vbs很丰富的内容. 所有vbs脚本都需要保存为.vbs形式才可以运行 FileSystemObject Object的所有方法参考:http:/ ...

  2. Using Dtrace OEL 6.X

    http://www.hhutzler.de/blog/using-dtrace/ http://docs.oracle.com/cd/E37670_01/E38608/html/dt_sdtparg ...

  3. Html学习笔记3

    1表格的标题和表头: <table> <caption>成绩单</caption> <tr> <th>姓名</th> <t ...

  4. 在ASP.NET MVC下限制同一个IP地址单位时间间隔内的请求次数

    有时候,当用户请求一个Controller下的Action,我们希望,在单位时间间隔内,比如每秒,每分钟,每小时,每天,每星期,限制同一个IP地址对某个Action的请求次数.如何做呢? stefan ...

  5. .net项目中使用Quartz

    (1)在web.config中进行相关配置 <configSections> <section name="quartz" type="System.C ...

  6. <c:otherwise>

    <c:if>没有<c:else>可以用<c:choose>来取代结构:<c:choose> <c:when test=""&g ...

  7. .NET:分布式事务

    背景 分布式事务使用起来比较方便,不过也是有成本的,因此如果可以不用就尽量不用,比如:采用saga.如果采用了分布式事务的话,就需要对分布式事务相关的几个概念有所了解. 分布式事务 相关角色: 事务发 ...

  8. svn导出文件进行比较

    之前有介绍svn log 的命令,即可导出版本A~B之间所有的修改动作,然后复制出相应的文件(中间有一个算法去处理每一个动作,然后得到最终需要导出的文件列表,svn常用动作有:Modified.Add ...

  9. LockSupport的park和unpark的基本使用,以及对线程中断的响应性

    LockSupport是JDK中比较底层的类,用来创建锁和其他同步工具类的基本线程阻塞原语.java锁和同步器框架的核心AQS:AbstractQueuedSynchronizer,就是通过调用Loc ...

  10. 实用ExtJS教程100例-004:等待对话框Ext.MessageBox.wait

    在前面两节中,我们分别演示了ExtJS三种常用的对话框和ExtJS带有进度条的对话框.在本节内容中,我们来看看ExtJS中的等待对话框. 首先来看一个简单的例子[查看在线示例]: 这种对话框会一直滚动 ...