按概率输出相应的随机数
2016-01-01 19:25
411 查看
#pragma strict
var probArray :float[];
private var probValue:int;
probArray = {0.5,0.25,0.05,0.02};
function OnGUI(){
if (GUI.Button(Rect(10,70,50,30),"Click"))
{
probValue = Choose(probArray);
switch(probValue){
case 0:
Debug.Log("NPC向我敬礼了");
break;
case 1:
Debug.Log("NPC离开了");
break;
case 2:
Debug.Log("NPC打我了");
break;
case 3:
Debug.Log("NPC给我钱了");
break;
default:
Debug.Log("我没碰到NPC");
}
}
}
function Choose(probs: float[]):int {
var total = 0.0;
for (elem in probs) {
// Debug.Log(elem);
total += elem;
// Debug.Log(total);
}
var randomPoint = Random.value * total;
var i:int;
for (i = 0; i < probs.Length;i++) {
if (randomPoint < probs[i])
return i;
else
randomPoint -= probs[i];
}
return probs.Length - 1;
}
var probArray :float[];
private var probValue:int;
probArray = {0.5,0.25,0.05,0.02};
function OnGUI(){
if (GUI.Button(Rect(10,70,50,30),"Click"))
{
probValue = Choose(probArray);
switch(probValue){
case 0:
Debug.Log("NPC向我敬礼了");
break;
case 1:
Debug.Log("NPC离开了");
break;
case 2:
Debug.Log("NPC打我了");
break;
case 3:
Debug.Log("NPC给我钱了");
break;
default:
Debug.Log("我没碰到NPC");
}
}
}
function Choose(probs: float[]):int {
var total = 0.0;
for (elem in probs) {
// Debug.Log(elem);
total += elem;
// Debug.Log(total);
}
var randomPoint = Random.value * total;
var i:int;
for (i = 0; i < probs.Length;i++) {
if (randomPoint < probs[i])
return i;
else
randomPoint -= probs[i];
}
return probs.Length - 1;
}
相关文章推荐
- 数据源架构模式之活动记录
- 【Java EE 学习 81】【CXF框架】【CXF整合Spring】
- leetcode 303. Range Sum Query - Immutable
- CentOs中的Mysql 下载安装
- 【0】Windows游戏安全之路——个人学习背景
- 欢迎使用CSDN-markdown编辑器
- 记——删除了mysql root@127.0.0.1恢复
- 数组
- LeetCode173 Binary Search Tree Iterator
- 【慕课笔记】第一章 JAVA初体验 第1节 JAVA平台应用
- 第八届河南省赛B.最大岛屿(dfs)
- 面向对象设计原则---迪米特法则
- 2016新年快乐
- 编码
- git中多人协同开发
- zzuoj--10400--海岛争霸(并查集)
- 2015总结
- char to int
- windows下建立文件的换行符^M导致linux下的shell脚本执行错误的解决方式
- zzuoj--10400--海岛争霸(并查集)