问题描述
大家好!,
有没有人知道网站的链接或知道创建数据的最佳方式
结构将挑选随机元素从本身分配了
概率?
例如,假设我创建了一个包含3个大理石(红色,绿色和蓝色)的包和
红色大理石被挑选的变化为50%,而绿色和蓝色大理石
都有25%的机会被挑选。它们应该是随机挑选的,但是这样选择的方式是红色大理石的选择时间是一半,而另外两个季度则是b $ b。什么是最好的方法呢?
算法?
谢谢,
David
Hi All!,
Does anyone know a link to a website or know the best way to create a data
structure that will pick random elements from itself with assigned
probabilities?
For example, say I created a bag of 3 marbles(red, green, and blue) and the
red marble had a 50% change of being picked while the green and blue marbles
both had 25% chance of being picked. they should be picked randomly, but
picked in such a way that the red marble will be picked half the time and
the other 2 a quarter of the time. What is the best way to do this
algorithmically?
Thanks,
David
or*******@yahoo.com
推荐答案
我会这样做,例如,创建一个
4个元素,0和1 =''红色'',2 =''绿色''和3 =''蓝色'',然后
随机选择元素0到3均匀。
SteveE
The way I would do it, for your example, would be to create an array of
4 elements, with 0 and 1 = ''red'', 2 = ''green'' and 3 = ''blue'', then
randomly select elements 0 to 3 evenly.
SteveE
如果你只需要两个分区,你可能会能够使用二叉树烹饪某些东西
。
If you only need divisions of two, you might be able to cook up somthing
using a binary tree.
嗨那里
u可以保持阵列具有各自概率的节点&一个
数组,用于维持节点被选择的次数,直到时间和时间为止。然后
生成一个随机数&检查是否有时间>没有时间它应该选择
然后生成另一个没有选择那个节点
这就是它
问候
amey
hi there
u can maintain array of nodes with their respective probabilities & an
array to maintain how many times the node was chosen till time & then
generate a random number & check if the no of times > no of times it
should have been chosen then generate another no else chose that node
this is it
regards
amey
这篇关于概率数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!