我目前正在创建一个教育游戏,以帮助人们学习数学,但偶然发现一个问题。C#确保随机数的存在
我随机化了玩家点击的一些立方体内的文本来测试他们的倍数知识,但是我现在使用的代码使得它偶尔在屏幕上没有正确的答案,是否有我可以解决这个问题吗?
代码:
按钮
using UnityEngine;
using System.Collections;
public class RNG : MonoBehaviour
{
public GUIText thisAnswer;
public RandomiseAll reRoll;
int randomNumber = 0;
int miniScore = 0;
// Use this for initialization
void Awake()
{
randomNumber = Random.Range (0, 36);
thisAnswer.text = randomNumber.ToString();
}
void OnMouseUpAsButton()
{
if (randomNumber % 3 == 0) {
miniScore += 100;
reRoll.Randomise();
Debug.Log (miniScore.ToString());
} else if (randomNumber % 3 != 0) {
reRoll.Randomise();
Debug.Log (miniScore.ToString());
}
}
}
全球重新随机化
using UnityEngine;
using System.Collections;
public class RandomiseAll : MonoBehaviour {
public GUIText[] answer;
int randomNumber;
int[] test;
public void Randomise(){
for (int i = 0; i < answer.Length; i++) {
randomNumber = Random.Range (0, 36);
answer [i].text = randomNumber.ToString();
}
}
}
任何帮助将不胜感激。
不要你重复的答案呢? – 2014-09-12 10:57:25
除非你正在做一个反向测验(Jeopardy),'thisAnswer.text = randomNumber.ToString();'是非常错误的。 – 2014-09-12 11:47:43