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 ...
随机推荐
- python获取本机IP、mac地址、计算机名
在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 我们先来看一下python 获得本机MAC地址: 1 2 3 4 import uuid de ...
- HDU 4287 Intelligent IME
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Python分类统计数据
在数据的常见分布中,有一种是一对多存储的数据,即一个是key,其他改key对应的多个value.例如气象数据等,每天有很多组,又或者是一个球员,他每天得多少分等等.我做这个东西有三种方法,即:常规编程 ...
- 转:SVN使用教程总结
转自:http://www.cnblogs.com/tugenhua0707/p/3969558.html SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版 ...
- openstack初探
一 .openstack三大核心功能: 计算--Nova.存储--Cinder.网络--Neutron. Nova:提供了计算资源的管理,可以管理跨服务网络的VM实例.还提供对多种Hypervisor ...
- 怎么在后台修改前台html页面的key、title、description
public void UpdateMeta(string title, string keyword, string desc) { ; i >= ; i--) { if (this.Head ...
- updatepanel的属性
updatepanel的属性 1.childrenastriggers:内容模板内的子控件的回发是否更新本模板(和updatemode的conditional有关) 2.updatemode:内容模板 ...
- 最全ASCLL码
结果 描述 实体编号 space ! exclamation mark ! " quotation mark " # number sign # $ dollar sign $ ...
- 苹果被拒的血泪史。。。(update 2015.11)
项目提交了N此了,也审核N次了,苹果的审核机制依旧那么不急不慢.昨天刚刚又被拒了.回忆下之前的,总结一下吧. 2015.04.28 昨天被拒非常亏,app的评级是17+,但是在app展示图里有一个比较 ...
- HBase 学习之一 <<HBase使用客户端API动态创建Hbase数据表并在Hbase下导出执行>>
HBase使用客户端API动态创建Hbase数据表并在Hbase下导出执行 ----首先感谢网络能够给我提供一个开放的学习平台,如果没有网上的技术爱好者提供 ...