概率算法1
1、50%的機會友好的問候?
2、25%的幾率走開?
3、20%的機會立即攻擊?
4、5%的機會提供金錢作為禮物?
下面的這個算法就是跟據概率數組,返回選擇的概率索引號。
?
int?Choose(float[] 概率數組)
{
???? float total=0;
??? //首先計算出概率的總值,用來計算隨機范圍
???? for(int i=0;i<概率數組.length;i++)
???? {
?????????? total+=概率數組[i];
???? }
??? Random rd = new Random();
??? float 隨機值=rd.Next(0,total);
??? for(int i=0;i<概率數組.length;i++)
??? {
??????????? if(隨機值<概率數組[i])
??????????? {
??????????????????? return i;
??????????? }
??????????? else
??????????? {
????????????????????隨機值-=概率數組[i];
????????????}
??? }
?????return 概率數組.length-1;
}
轉載于:https://www.cnblogs.com/lx-hhxxttxs/p/4961517.html
總結
- 上一篇: Linux_Centos中搭建nexus
- 下一篇: 信息安全系统设计基础第九周学习总结