English 中文(简体)
a. 向一些有偏差的ger类动物提供随机生成物 b
原标题:Biasing random number generator to some integer n with deviation b

鉴于R=[a, b](如果是“一个”的,=0和b'lt;=100),R中带有偏见的分类,以及一些偏差的b,我可以采用何种公式将随机的生成物推向N?

例如,如果我有1至10个包容性的数字,而且我没有具体说明一个有偏见的数字,那么在理论上,我就应该有同样的机会随意抽取其中一人。

但是,如果我确实给出了具体的偏差数字(例如,3),那么,创建者的人数应当比其他数字要多出3个。

如果我具体说明除了有偏见的号码外还有第2号声明的偏离,那么,发电机的数目应当从1到5, 更经常地从6到10。

我能用什么算法来做到这一点?

如果能够方便/更硬地使用Ruby。

最佳回答

i 认为最简单的路线是从正常(ka gaussian)分配到你想要的财产,然后改变结果:

  • generate a normal value with given mean and sd
  • round to nearest integer
  • if outside given range (normal can generate values over the entire range from -infinity to -infinity), discard and repeat

如果你需要从制服中产生正常变化,最简单的转变是“箱-muller

某些细节可能需要担心。 尤其是,箱子 mu在范围上是有限的(它不会产生极不可能的价值,永远也不会产生)。 如果你提供范围非常狭窄,那么你就永远不会获得全部价值。 其它变化并不有限,即建议使用任何废墟(指“正常”或“俄罗斯”)。

also, be careful to round the value. 2.6 to 3.4 should all become 3, for example. if you simply discard the decimal (so 3.0 to 3.999 become 3) you will be biased.

如果你真正关心效率,不想抛弃价值观,那么你就能够发明一些东西。 一种方式是将一个具有偏见价值的统一体组合起来(即9/10倍产生制服,1/10倍回报3)。 在某些情况下,如果你只关注抽样的平均数,那就足够了。

问题回答

就第一部分而言,“如果我确实给出了具体的偏差数字(例如,3),那么,发电机的数量应当比其他数字要多出3个,这是一个非常容易的解决办法:

def randBias(a,b,biasedNum=None, bias=0):
   x = random.randint(a, b+bias)
   if x<= b:
       return x
   else:
       return biasedNum

就第二部分而言,我要说这取决于这项任务。 如果你需要从同一分配中产生10亿个随机数字,我将明确计算数字的概率,并使用加权随机数字生成器(见)。 兰德曼加权选择

如果你想要单一模式分配(如果这种偏差只是集中在你人数的某个特定价值上,例如,正如你指出的3个),那么由oke子提供的答复是好的,因为它使你能够非常准确地调整偏离。

然而,如果你想作出一些偏颇的表示,例如,你想要进行三式分配,其编号为A、(a+b)/2和比其他人更经常,而执行加权随机选择则好。

a 最近关于StackOverflow的问题;其复杂性是直线性的。 采用这种算法,你将仅保留一份清单,初步清单包括{a, a+1, a+2,..., b-1, b}(大小 b-a+1),如果你想增加一个偏见,来对待。 第十,根据你想要多少偏见,你会向名单上的X多份。 然后从名单上抽取一个随机项目。

如果你想要提高效率的话,最有效的方法被称为“Alias”方法,即:,在Denis Bzowy的严格实施;一旦对你的阵列进行了预先处理,便会随时进行(但这意味着,一旦你做初步处理后,你会再更新偏见。

The downside with both techniques is that unlike with the Gaussian distribution, biasing towards X, will not bias also somewhat towards X-1 and X+1. To simulate this effect you would have to do something such as

def addBias(x, L):
   L = concatList(L, [x, x, x, x, x])
   L = concatList(L, [x+2])
   L = concatList(L, [x+1, x+1]) 
   L = concatList(L, [x-1,x-1,x-1])
   L = concatList(L, [x-2])




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签