Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

javascript - JS反转概率(JS inverting probabilities)

I'm generating a grid of objects, each of which has one of 3 colors.(我正在生成一个对象网格,每个对象都有3种颜色之一。)

Assume I'm filling a particular grid cell.(假设我正在填充特定的网格单元。) I know that around this cell there are, for example, 1 object of color 0 , 1 of color 1 and 2 of color 2 , so:(我知道这个细胞周围有,例如,颜色的1个物体0 ,颜色的1 1和色2 2 ,所以:) const surroundings = { 0: 1, 1: 1, 2: 2 } Now I want to assign a color to the current cell I'm working with.(现在,我想为正在使用的当前单元格分配颜色。) I need to count probabilities for each color to be used.(我需要计算要使用的每种颜色的概率。) To make it look pleasing, I want it more likely to be different from its surroundings.(为了使它看起来令人愉悦,我希望它与周围环境有所不同。) So in the case above the probabilities could be { 0: 0.4, 1: 0.4, 2: 0.2 } .(因此,在上述情况下,概率可能是{ 0: 0.4, 1: 0.4, 2: 0.2 } 。) I'm sure there is an implementation of such operation in the probability theory, but I'm having trouble finding the correct term for it.(我敢肯定,概率论中有这样一种运算的实现,但是我很难找到它的正确术语。) In the example I gave the probabilities could be different, because I have no idea how to calculate them.(在示例中,我给出的概率可能不同,因为我不知道如何计算它们。) But for colors 0 and 1 they should certainly be equal and for color 2 it should be the smallest.(但是对于颜色01它们肯定应该相等,而对于颜色2它应该是最小的。)   ask by Alex Chashin translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could get the reciprocal and the sum and return the part of it.(您可以获取倒数和总和,然后返还一部分。)

function getPro(array) { var inv = array.map(v => 1 / v), sum = inv.reduce((a, b) => a + b); return inv.map(v => v / sum); } console.log(getPro([1, 1, 2])); console.log(getPro([1, 1, 1])); console.log(getPro([2, 4, 6]));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...