Algorithm | Random
随机生成[0,n)中不重复的m个数。
class Random {
public:
Random(int n, int m):n(n), m(m) {}
void generate() {
srand(time(NULL));
for (int i = ; i < n; ++i) data.push_back(i);
for (int i = ; i < m; ++i) swap(data[i], data[i + rand() % (n - i)]);
}
void increase() {
data.push_back(n++);
int i = rand() % n;
swap(data[i], data[n - ]);
}
void print() const {
for (int i = ; i < m; ++i)
cout << data[i] << " ";
cout << endl;
}
private:
int n;
int m;
vector<int> data;
};
Line 7, 第i个元素会和第从[i, n)中找一个元素交换。可以证明每个数被选择在第k个位置上的概念为1/n。被选择的概念为m/n。
比如0被放在data[1]这个位置的概率,等于0没有被放在第一个位置的概率*0放在第二个位置的概率,也就是(1-1/n)*1/(n-1)=1/n。
依此类似。
最终前m个数就是不重复随机数。
如果data是不断递增的,也就是newN = n + 1,怎么随机选择k个数? 只要将data[newN - 1]和前面的数随机交换就可以了,O(1)的更新。
对于第newN个数,它被选上的概率是1/newN.
其他数被选上的概率 =(它之前被选在m个数里面的概率)*(它在newN下没有被交换出这m个数的概率)
=(它之前被选在m个数里面的概率)*(第newN个数和其他数交换的概率)=(1/n)*(n/newN)=1/newN。
Select a random number from stream, with O(1) space
Given a stream of numbers, generate a random number from the stream. You are allowed to use only O(1) space and the input is in the form of stream, so can’t store the previously seen numbers.
So how do we generate a random number from the whole stream such that the probability of picking any number is 1/n. with O(1) extra space? This problem is a variation of Reservoir Sampling. Here the value of k is 1.
看完geeksforgeeks上面的解释。
重写了一遍。
void increase() {
data.push_back(n++);
int i = rand() % n;
//swap(data[i], data[n - 1]);
if (i < m) data[i] = data[n - ];
}
Algorithm | Random的更多相关文章
- 机器学习算法 --- Pruning (decision trees) & Random Forest Algorithm
一.Table for Content 在之前的文章中我们介绍了Decision Trees Agorithms,然而这个学习算法有一个很大的弊端,就是很容易出现Overfitting,为了解决此问题 ...
- Graphs and Minimum Cuts(Karger's Min-Cut Algorithm)
Graphs Two ingredients 1. vertices (nodes) v 2. edges(undirected or directed) Examples: road networ ...
- ML—随机森林·1
Introduction to Random forest(Simplified) With increase in computational power, we can now choose al ...
- TensorFlow利用A3C算法训练智能体玩CartPole游戏
本教程讲解如何使用深度强化学习训练一个可以在 CartPole 游戏中获胜的模型.研究人员使用 tf.keras.OpenAI 训练了一个使用「异步优势动作评价」(Asynchronous Advan ...
- DRL 教程 | 如何保持运动小车上的旗杆屹立不倒?TensorFlow利用A3C算法训练智能体玩CartPole游戏
本教程讲解如何使用深度强化学习训练一个可以在 CartPole 游戏中获胜的模型.研究人员使用 tf.keras.OpenAI 训练了一个使用「异步优势动作评价」(Asynchronous Advan ...
- cvpr2015papers
@http://www-cs-faculty.stanford.edu/people/karpathy/cvpr2015papers/ CVPR 2015 papers (in nicer forma ...
- 从Random Walk谈到Bacterial foraging optimization algorithm(BFOA),再谈到Ramdom Walk Graph Segmentation图分割算法
1. 从细菌的趋化性谈起 0x1:物质化学浓度梯度 类似于概率分布中概率密度的概念.在溶液中存在不同的浓度区域. 如放一颗糖在水盆里,糖慢慢溶于水,糖附近的水含糖量比远离糖的水含糖量要高,也就是糖附近 ...
- [Machine Learning & Algorithm] 随机森林(Random Forest)
1 什么是随机森林? 作为新兴起的.高度灵活的一种机器学习算法,随机森林(Random Forest,简称RF)拥有广泛的应用前景,从市场营销到医疗保健保险,既可以用来做市场营销模拟的建模,统计客户来 ...
- 醉汉随机行走/随机漫步问题(Random Walk Randomized Algorithm Python)
世界上有些问题看似是随机的(stochastic),没有规律可循,但很可能是人类还未发现和掌握这类事件的规律,所以说它们是随机发生的. 随机漫步(Random Walk)是一种解决随机问题的方法,它 ...
随机推荐
- 笔记-python-standard library-8.1 data types-datetime
笔记-python-standard library-8.1 data types-datetime 1. Datatimes 本章节内容为日期和时间处理类和方法. 1.1. date ...
- Diycode开源项目 NotificationActivity
1.NotificationActivity预览以及布局详解 1.1.首先看一下通知的具体页面. 1.2.然后是布局代码==>activity_fragment.xml <LinearLa ...
- 在MAC下使用Robotframework+Selenium2【第一枪】robotframework安装步骤
最近使用苹果的MAC Pro本本,感受着苹果系统的新鲜,确实让我手忙脚乱一阵,毕竟使用windows系统太长时间了,刚开始用MAC Pro确实感觉别扭,用了一段,发现MAC系统还不错,好了,转入正题. ...
- [项目2] 10mins
1.准备工作 M层:生成虚假数据 from django.db import models from faker import Factory # Create your models here. c ...
- Falsk
flask: 1.配置文件的几种方式: 1.app.config['DEBUG'] =True 2.app.config.from_pyfile("setting.py") 3.a ...
- 【Letter Combinations of a Phone Number】cpp
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- 【Gas Station】cpp
题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- mvc-自定义视图引擎
//自定义视图引擎的实质是把数据模型(moudle)和模板(View)转换成html页面,输出到客户端public class MyView:IView { string _viewPath; pub ...
- web自动化测试:watir+minitest(一)
基本介绍: 本课程的测试环境和工具为:win7+ruby+watir+minitest Watir 全称是"Web Application Testing in Ruby".它是一 ...
- hihoCoder 1467 2-SAT·hihoCoder音乐节(2-SAT模版)
#1467 : 2-SAT·hihoCoder音乐节 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 hihoCoder音乐节由hihoCoder赞助商大力主办,邀请了众 ...