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的更多相关文章

  1. 茅坑杀手与Alias Method离散采样

    说起Alias,你可能第一个联想到的是Linux中的Alias命令,就像中世纪那些躲在茅坑下面(是真的,起码日本有粪坑忍者,没有马桶的年代就是社会的噩梦)进行刺杀的杀手一样,让人防不胜防,对于那些被这 ...

  2. Alias Method解决随机类型概率问题

    举个例子,游戏中玩家推倒了一个boss,会按如下概率掉落物品:10%掉武器 20%掉饰品 30%掉戒指 40%掉披风.现在要给出下一个掉落的物品类型,或者说一个掉落的随机序列,要求符合上述概率. 一般 ...

  3. java加密类型和算法名称

    项目里有各种加密方法,但从来没有仔细研究过.一般只是copy.这几天遇到一些问题,看了一下加密代码,觉得有些疑惑. 我们知道jdk已经为我们包装好了很多的算法.但究竟包装了哪些算法,怎么去掉这些算法我 ...

  4. Alias Method for Sampling 采样方法

    [Alias Method for Sampling]原理 对于处理离散分布的随机变量的取样问题,Alias Method for Sampling 是一种很高效的方式. 在初始好之后,每次取样的复杂 ...

  5. 封装算法: 模板方法(Template Method)模式

    template method(模板方法)模式是一种行为型设计模式.它在一个方法中定义了算法的骨架(这种方法被称为template method.模板方法),并将算法的详细步骤放到子类中去实现.tem ...

  6. paper 142:SDM算法--Supervised Descent Method

    对于face recognition的研究,我是认真的(认真expression,哈哈哈~~~~~~)许久没有写blog了,欢迎一起讨论. SDM(Supvised Descent Method)方法 ...

  7. 三维网格补洞算法(Poisson Method)

    下面介绍一种基于Poisson方程的三角网格补洞方法.该算法首先需要根据孔洞边界生成一个初始化补洞网格,然后通过法向估算和Poisson方程来修正补洞网格中三角面片的几何形状,使其能够适应并与周围的原 ...

  8. 三维网格补洞算法(Poisson Method)(转载)

    转载:https://www.cnblogs.com/shushen/p/5864042.html 下面介绍一种基于Poisson方程的三角网格补洞方法.该算法首先需要根据孔洞边界生成一个初始化补洞网 ...

  9. c#中奖算法的实现

    算法名称 Alias Method public class AliasMethod { /* The probability and alias tables. */ private int[] _ ...

随机推荐

  1. npm 安装、卸载 模块或包的命令(转载)

    npm安装卸载命令 来源:https://www.jianshu.com/p/e6ee00ea03cd npm安装模块 [npm install xxx]利用 npm 安装xxx模块到当前命令行所在目 ...

  2. Layui外部js修改表格内容

    //测试修改数据的方法! var _tds=$(".layui-table-body.layui-table-main:eq(1) tr:eq(1)").children(); _ ...

  3. java 获取最近7天 最近今天的日期

    private static Date getDateAdd(int days){ SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-d ...

  4. ArcGIS操作技巧——怎样把地图放到PPT中,并且进行编辑?

    需求:把arcgis配好的矢量地图插入到ppt中,并且要求可以在PPT中进行修改和重新着色.编辑. 效果:如下图所示: 操作过程: 方法一: 在最上面工具栏找到edit——>copy map t ...

  5. Kotlin协程作用域与Job详解

    Job详解: 在上一次https://www.cnblogs.com/webor2006/p/11725866.html中抛出了一个问题: 所以咱们将delay去掉,需要改造一下,先把主线程的dela ...

  6. 【转载】lr运行时设置,每个action 比例

    提供了再脚本运行时所需要的相关选项. 性能测试的关键之一:能否通过脚本来完全模拟用户的行为,可以通过运行设置让脚本运行的更人性化. 1. Run Logic 脚本如何运行,每个action与actio ...

  7. python测试开发django-rest-framework-65.序列化(ModelSerializer)

    前言 serializers.Serializer可以对modle模型中的字段序列化,并且必须写create和update两个方法.ModelSerializer可以看成是Serializer的一个升 ...

  8. tensorflow Dataset及TFRecord一些要点【持续更新】

    关于tensorflow结合Dataset与TFRecord这方面看到挺好一篇文章: https://cloud.tencent.com/developer/article/1088751 githu ...

  9. 《exception》第九次团队作业:Beta冲刺与验收准备(大结局)

    一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.掌握软件黑盒测试技术:2.学会编制软件项目 ...

  10. 《CoderXiaoban》第八次团队作业:Alpha冲刺

    项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 实验十二 团队作业8:软件测试与ALPHA冲刺 团队名称 Coderxiaoban团队 作业学习目标 (1)掌握软件测试基 ...