reservoir sampling / random shuffle
randomly choose a sample of k items from a list S containing n elements, the algorithm may be online (i.e. the input list is unknown beforehand)
https://en.wikipedia.org/wiki/Reservoir_sampling
ReserviorSampling(Source[..n], Result[..k]) {
for (int i = ; i <= k; i++) {
Result[i] = Source[i];
}
for (int i = k+; i <= n; i++) {
int rand = Random.get(, i); // both 1 and i are inclusive
if (rand <= k) {
Result[rand] = Source[i];
}
}
return Result;
}
vector<int> shuffle(const vector<int> &nums) {
auto ret = nums;
int n = ret.size();
for (int i = ; i < n; i++) {
int s = rand()%(n-i)+i;
swap(ret[i], ret[s]);
}
return ret;
}
reservoir sampling / random shuffle的更多相关文章
- Reservoir Sampling - 蓄水池抽样问题
问题起源于编程珠玑Column 12中的题目10,其描述如下: How could you select one of n objects at random, where you see the o ...
- 算法系列:Reservoir Sampling
copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...
- 蓄水池采样算法(Reservoir Sampling)
蓄水池采样算法 问题描述分析 采样问题经常会被遇到,比如: 从 100000 份调查报告中抽取 1000 份进行统计. 从一本很厚的电话簿中抽取 1000 人进行姓氏统计. 从 Google 搜索 & ...
- Reservoir Sampling - 蓄水池抽样
问题起源于编程珠玑Column 12中的题目10,其描述如下: How could you select one of n objects at random, where you see the o ...
- 水塘抽样(Reservoir Sampling)问题
水塘抽样是一系列的随机算法,其目的在于从包含n个项目的集合S中选取k个样本,其中n为一很大或未知的数量,尤其适用于不能把所有n个项目都存放到主内存的情况. 在高德纳的计算机程序设计艺术中,有如下问题: ...
- Spark MLlib之水塘抽样算法(Reservoir Sampling)
1.理解 问题定义可以简化如下:在不知道文件总行数的情况下,如何从文件中随机的抽取一行? 首先想到的是我们做过类似的题目吗?当然,在知道文件行数的情况下,我们可以很容易的用C运行库的rand函数随机的 ...
- [Algorithm] Reservoir Sampling
Given a stream of elements too large to store in memory, pick a random element from the stream with ...
- 【算法34】蓄水池抽样算法 (Reservoir Sampling Algorithm)
蓄水池抽样算法简介 蓄水池抽样算法随机算法的一种,用来从 N 个样本中随机选择 K 个样本,其中 N 非常大(以至于 N 个样本不能同时放入内存)或者 N 是一个未知数.其时间复杂度为 O(N),包含 ...
- 蓄水池抽样算法 Reservoir Sampling
2018-03-05 14:06:40 问题描述:给出一个数据流,这个数据流的长度很大或者未知.并且对该数据流中数据只能访问一次.请写出一个随机选择算法,使得数据流中所有数据被选中的概率相等. 问题求 ...
随机推荐
- 函数模板&类模板
#include <iostream> #if 0//函数模板 template<typename T> T max(T a, T b, T c)//函数模板 { if (a ...
- 【BZOJ4569】[Scoi2016]萌萌哒 倍增+并查集
[BZOJ4569][Scoi2016]萌萌哒 Description 一个长度为n的大数,用S1S2S3...Sn表示,其中Si表示数的第i位,S1是数的最高位,告诉你一些限制条件,每个条件表示为四 ...
- Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)
sftp -b batchfile username@remote_host 报错:Permission denied (publickey,gssapi-keyex,gssapi-with-mic, ...
- 九度OJ 1003:A+B
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:15078 解决:6299 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开. 现在请计 ...
- (转)CentOS 5.5 64bit 编译安装Adobe FlashMediaServer 3.5
http://download.macromedia.com/pub/flashmediaserver/updates/4_0_2/Linux_32bit/FlashMediaServer4.tar. ...
- 可视化工具与pymongo
可视化工具 链接:https://robomongo.org/ pymongo 官网:http://api.mongodb.com/python/current/tutorial.html from ...
- android 电池(一):锂电池基本原理篇【转】
本文转载自:http://blog.csdn.net/xubin341719/article/details/8497830 关键词:Android 电池关机充电 androidboot.mode ...
- 图解HTTP接口自动化测试框架使用
Robot Framework是一款python语言编写,通用的功能自动化测试框架.它使用了比较易用的表格数据语法,基于关键字驱动测试,主要用来验收测试和验收测试驱动开发(ATDD). 运行RIDE, ...
- BZOJ 1612 [Usaco2008 Jan]Cow Contest奶牛的比赛:floyd传递闭包
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1612 题意: 有n头牛比赛. 告诉你m组(a,b),表示牛a成绩比牛b高. 保证排名没有并 ...
- python学习笔记:第五天( 字典)
Python3 字典 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格 ...