算法名称 Alias Method
public class AliasMethod {
/* The probability and alias tables. */
private int[] _alias;
private double[] _probability; public AliasMethod(List<Double> probabilities) { /* Allocate space for the probability and alias tables. */
_probability = new double[probabilities.Count];
_alias = new int[probabilities.Count]; /* Compute the average probability and cache it for later use. */
double average = 1.0 / probabilities.Count; /* Create two stacks to act as worklists as we populate the tables. */
var small = new Stack<int>();
var large = new Stack<int>(); /* Populate the stacks with the input probabilities. */
for (int i = ; i < probabilities.Count; ++i) {
/* If the probability is below the average probability, then we add
* it to the small list; otherwise we add it to the large list.
*/
if (probabilities[i] >= average)
large.Push(i);
else
small.Push(i);
} /* As a note: in the mathematical specification of the algorithm, we
* will always exhaust the small list before the big list. However,
* due to floating point inaccuracies, this is not necessarily true.
* Consequently, this inner loop (which tries to pair small and large
* elements) will have to check that both lists aren't empty.
*/
while (small.Count > && large.Count > ) {
/* Get the index of the small and the large probabilities. */
int less = small.Pop();
int more = large.Pop(); /* These probabilities have not yet been scaled up to be such that
* 1/n is given weight 1.0. We do this here instead.
*/
_probability[less] = probabilities[less] * probabilities.Count;
_alias[less] = more; /* Decrease the probability of the larger one by the appropriate
* amount.
*/
probabilities[more] = (probabilities[more] + probabilities[less] - average); /* If the new probability is less than the average, add it into the
* small list; otherwise add it to the large list.
*/
if (probabilities[more] >= average)
large.Push(more);
else
small.Push(more);
} /* At this point, everything is in one list, which means that the
* remaining probabilities should all be 1/n. Based on this, set them
* appropriately. Due to numerical issues, we can't be sure which
* stack will hold the entries, so we empty both.
*/
while (small.Count > )
_probability[small.Pop()] = 1.0;
while (large.Count > )
_probability[large.Pop()] = 1.0;
} /**
* Samples a value from the underlying distribution.
*
* @return A random value sampled from the underlying distribution.
*/
public int next() { long tick = DateTime.Now.Ticks;
var seed = ((int)(tick & 0xffffffffL) | (int)(tick >> ));
unchecked {
seed = (seed + Guid.NewGuid().GetHashCode() + new Random().Next(, ));
}
var random = new Random(seed);
int column = random.Next(_probability.Length); /* Generate a biased coin toss to determine which option to pick. */
bool coinToss = random.NextDouble() < _probability[column]; return coinToss ? column : _alias[column];
}
}
Dictionary<String, Double> map = new Dictionary<String, Double>();
map.Add("1金币", 0.2);
map.Add("2金币", 0.15);
map.Add("3金币", 0.1);
map.Add("4金币", 0.05);
map.Add("未中奖", 0.5); List<Double> list = new List<Double>(map.Values);
List<String> gifts = new List<String>(map.Keys); AliasMethod method = new AliasMethod(list); Dictionary<String, int> resultMap = new Dictionary<String, int>(); for (int i = ; i < ; i++) {
int index = method.next();
string key = gifts[index];
Console.WriteLine(index+":"+key);
}
源文:https://www.cnblogs.com/ahjesus/p/6038015.html
算法名称 Alias Method的更多相关文章
- 茅坑杀手与Alias Method离散采样
说起Alias,你可能第一个联想到的是Linux中的Alias命令,就像中世纪那些躲在茅坑下面(是真的,起码日本有粪坑忍者,没有马桶的年代就是社会的噩梦)进行刺杀的杀手一样,让人防不胜防,对于那些被这 ...
- Alias Method解决随机类型概率问题
举个例子,游戏中玩家推倒了一个boss,会按如下概率掉落物品:10%掉武器 20%掉饰品 30%掉戒指 40%掉披风.现在要给出下一个掉落的物品类型,或者说一个掉落的随机序列,要求符合上述概率. 一般 ...
- java加密类型和算法名称
项目里有各种加密方法,但从来没有仔细研究过.一般只是copy.这几天遇到一些问题,看了一下加密代码,觉得有些疑惑. 我们知道jdk已经为我们包装好了很多的算法.但究竟包装了哪些算法,怎么去掉这些算法我 ...
- Alias Method for Sampling 采样方法
[Alias Method for Sampling]原理 对于处理离散分布的随机变量的取样问题,Alias Method for Sampling 是一种很高效的方式. 在初始好之后,每次取样的复杂 ...
- 封装算法: 模板方法(Template Method)模式
template method(模板方法)模式是一种行为型设计模式.它在一个方法中定义了算法的骨架(这种方法被称为template method.模板方法),并将算法的详细步骤放到子类中去实现.tem ...
- paper 142:SDM算法--Supervised Descent Method
对于face recognition的研究,我是认真的(认真expression,哈哈哈~~~~~~)许久没有写blog了,欢迎一起讨论. SDM(Supvised Descent Method)方法 ...
- 三维网格补洞算法(Poisson Method)
下面介绍一种基于Poisson方程的三角网格补洞方法.该算法首先需要根据孔洞边界生成一个初始化补洞网格,然后通过法向估算和Poisson方程来修正补洞网格中三角面片的几何形状,使其能够适应并与周围的原 ...
- 三维网格补洞算法(Poisson Method)(转载)
转载:https://www.cnblogs.com/shushen/p/5864042.html 下面介绍一种基于Poisson方程的三角网格补洞方法.该算法首先需要根据孔洞边界生成一个初始化补洞网 ...
- c#中奖算法的实现
算法名称 Alias Method public class AliasMethod { /* The probability and alias tables. */ private int[] _ ...
随机推荐
- 缓存注解@Cacheable、@CacheEvict、@CachePut使用及注解失效时间
从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...
- IDEA如何配置tomcat及建一个web项目
需求:项目工程可能别人已经建好了,我需要把项目导入到自己的IDEA并配置tomcat运行; 准备工作:1.本地的tomat服务器 2.IDEA工具 3.JDK 步骤: 1.点击Run -> Ed ...
- 24.centos7基础学习与积累-010-上机考核命令练习
从头开始积累centos7系统运用 大牛博客:https://blog.51cto.com/yangrong/p5 1.创建目录/data/oldboy,并且在该目录下创建文件oldboy.txt,然 ...
- 18.centos7基础学习与积累-004-分区理论
1.从头开始积累centos7系统运用 大牛博客:https://blog.51cto.com/yangrong/p5 1.常规分区:数据不是特别重要的业务(集群的某个节点) /boot 引导分区 ...
- Flume实战案例运维篇
Flume实战案例运维篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Flume概述 1>.什么是Flume Flume是一个分布式.可靠.高可用的海量日志聚合系统,支 ...
- node基础学习——http基础知识-01-客户单请求
<一> HTTP基础createServer()相关事件介绍 1. 创建HTTP服务器 server = http.createServer([requestListener]) // 下 ...
- beta版本——第五次冲刺
第五次冲刺 (1)SCRUM部分☁️ 成员描述: 姓名 李星晨 完成了哪个任务 界面优化 花了多少时间 2h 还剩余多少时间 2h 遇到什么困难 没有 这两天解决的进度 2/2 后续两天的计划 完成文 ...
- 米勒罗宾素数检测(Miller-Rabin)
适用范围:较大数的较快素性判断 思路: 因为有好的文章讲解具体原理(见参考文章),这里只是把代码的大致思路点一下,读完了文章如果还有些迷糊,可以参考以下解释 原理是费马小定理:如果p是素数,则a^(p ...
- Java动态代理演变之路
1.什么是代理? 代理,英文成文Proxy.意思是你不用去做,别人代替你去处理.比如有人想找明星周董去唱歌,他需要做签约.讨论.唱歌和付款等等过程,但真正周董擅长的事情是唱歌,其他的事情可以交代给他的 ...
- NumPy的Linalg线性代数库探究
1.矩阵的行列式 from numpy import * A=mat([[1,2,4,5,7],[9,12,11,8,2],[6,4,3,2,1],[9,1,3,4,5],[0,2,3,4,1]]) ...