select m objects from n objects randomly
Q: how to select m objects randomly from n objects with euqal possibility?
A: allocate an array of m elements to keep the final result.
put the first m objects into the array.
foreach object k numbered from m+1 to n, generate an random rational number a in [0, 1],
if a <= m/k then
generate an random integer number in [1, m], say i, remove the i-th element from the final result,
and put the kth number into it.
//PROVE the answer.
We can use mathematics induction.
The propositonal statement is P(n): the method in the answer can be used to select m objects out of n objects randomly, and each object is picked out with possibility of m/n, (n>=m).
Basis step:
P(m) is true since the answer put the first m elemets into the final result.
induction step:
The induction hypothesis is P(k) is true for all k>=m, then
the (k+1)-th element will be select with possibility of m/(k+1).
Any element kept in the final result before iteration k+1 has a possibility to be removed from the array which is m/(k+1) * (1/m), from the hypothesis, so it will stay in the final result with a possibility of m/k * (1 - m/(k+1) * (1/m)) = m/(k+1).
From the basis step and induction step, we have that P(n) is true for any integer n>=m.
select m objects from n objects randomly的更多相关文章
- 关于变量 Objects...objects 和Object[] objects的区别
上一篇用到Objects...objects 和Object[] objects的遇到点小问题,于是我去做了个实验,关于这两个变量传参的问题 代码如下 package com.yck.test; pu ...
- Django objects.all()、objects.get()与objects.filter()之间的区别介绍
前言 本文主要介绍的是关于Django objects.all().objects.get()与objects.filter()直接区别的相关内容,文中介绍的非常详细,需要的朋友们下面来一起看看详细的 ...
- django中的objects.get和objects.filter方法的区别
为了说明它们两者的区别定义2个models class Student(models.Model): name = models.CharField('姓名', max_length=20, defa ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十一)之Holding Your Objects
To solve the general programming problem, you need to create any number of objects, anytime, anywher ...
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- TIJ——Chapter Eleven:Holding Your Objects
Java Provides a number of ways to hold objects: An array associates numerical indexes to objects. It ...
- JavaScript- The Good Parts Chapter 3 Objects
Upon a homely object Love can wink.—William Shakespeare, The Two Gentlemen of Verona The simple type ...
- LINQ之LINQ to Objects(上)
LINQ概述 LINQ,语言集成查询(Language Integrated Query),它允许使用C#或VB代码以查询数据库相同的方式来操作不同的数据源. 1.LINQ体系结构 从上图可以看出,L ...
- 从LINQ开始之LINQ to Objects(上)
LINQ概述 LINQ,语言集成查询(Language Integrated Query),它允许使用C#或VB代码以查询数据库相同的方式来操作不同的数据源. LINQ体系结构 从上图可以看出,LIN ...
随机推荐
- git如何clone所有的远程分支
问题: 文/赖忠标 周末在家里改了下代码,新建了个angular版本的分支,然后push到coding.net上面了. 今天,到公司却不知道怎么拉取这个angular分支到公司的电脑上面.如下图(1) ...
- [转]【基于zxing的编解码实战】精简Barcode Scanner篇
通过<[搞定条形码]zxing项目源码解读(2.3.0版本,Android部分)>的分析,现在可以实现最终目标了:精简Barcode Scanner并将其中的编码和解码分离为两个独立的部分 ...
- scrollview中停止滚动的监听
[补充]可以在主线程控制,特别注意 scrollView3.postDelayed(new Runnable() { @Override public void run() { scrollView3 ...
- 卸载Symantec Endpoint Protection, 无需password的卸载方法
近期一次偶然的机会, 被人装了个Symantec在电脑上, 搞得各种报警, 验证, 烦死. 然后就自然而然的想卸载掉这个该死的杀毒软件, 没想到这个杀毒软件竟然还是个流氓杀毒软件, 卸载还须要pass ...
- 利用d3.js绘制雷达图
利用d3,js将数据可视化,能够做到数据与代码的分离.方便以后改动数据. 这次利用d3.js绘制了一个五维的雷达图.即将多个对象的五种属性在一张图上对照. 数据写入data.csv.数据类型写入typ ...
- 用htaccess进行访问控制(转)
1. 文件访问控制 利用 httpd.conf 中的 Order.Files 及 FilesMatch 命令实现的访问控制可以满足大部分要求,但是当用户被拒绝时,他们看到的是硕大的“403 Forbi ...
- java.util.jar.JarFile cause native heap memory leak
最近项目中使用了JarFile 这个类 来load jar包中的 configuration,大致的情况如下 public void processJarEntries(JarFile paramJa ...
- [转] 多线程下变量-gcc原子操作 __sync_fetch_and_add等
http://blog.sina.com.cn/s/blog_6f5b220601013zw3.html 非常好的原子操作,不用加锁:__sync_fetch_and_add GCC 提供的原子操作 ...
- 谈谈iOS中粘性动画以及果冻效果的实现
在最近做个一个自定义PageControl——KYAnimatedPageControl中,我实现了CALayer的形变动画以及CALayer的弹性动画,效果先过目: https://github.c ...
- 使用HTML5 WebDataBase设计离线数据库
基于HTML5的Web DataBase 可以让你在浏览器中进行数据持久地存储管理和有效查询,假设你的离线应用程序有需要规范化的存储功能,那么使用Web DataBase,可以使你的应用程序无论是在离 ...