每次生成随机数,不包括最后一个数字
Generate random number each time and don't include last number
我有4种颜色。我想让玩家不能连续 2 次出现相同的颜色。当玩家与物体发生碰撞时,将调用 RandomColor()
。所以这个函数在游戏过程中被调用了很多次,有时玩家并没有改变他的颜色。
using UnityEngine;
public class ColorManager : MonoBehaviour {
public SpriteRenderer player;
public string playerColor;
public Color[] colors = new Color[4];
private void Awake()
{
RandomColor();
}
public void RandomColor()
{
int index = Random.Range(0, 4);
switch (index)
{
case 0:
player.color = colors[0]; //colors[0] is orange
playerColor = "orange";
break;
case 1:
player.color = colors[1]; //colors[1] is pink
playerColor = "pink";
break;
case 2:
player.color = colors[2]; //colors[2] is blue
playerColor = "blue";
break;
case 3:
player.color = colors[3]; //colors[3] is purple
playerColor = "purple";
break;
}
}
}
尝试使用 while 循环,执行 while 循环,但我显然做错了,因为有时我会连续两次收到相同的颜色。如果有人能弄明白并解释它会很好,那就太好了 how/why 因为我在这个问题上花了很多时间而且我很好奇。
首先,你需要一个可以生成带排除的随机数的函数。以下是我为此使用的内容:
int RandomWithExclusion(int min, int max, int exclusion)
{
int result = UnityEngine.Random.Range(min, max - 1);
return (result < exclusion) ? result : result + 1;
}
每次调用时,需要将结果保存在全局变量中,以便下次调用时传递给exclusion
参数。
我修改了该函数,这样您就不必在每次调用它时都这样做。新的 RandomWithExclusion
功能将为您做到这一点。
int excludeLastRandNum;
bool firstRun = true;
int RandomWithExclusion(int min, int max)
{
int result;
//Don't exclude if this is first run.
if (firstRun)
{
//Generate normal random number
result = UnityEngine.Random.Range(min, max);
excludeLastRandNum = result;
firstRun = false;
return result;
}
//Not first run, exclude last random number with -1 on the max
result = UnityEngine.Random.Range(min, max - 1);
//Apply +1 to the result to cancel out that -1 depending on the if statement
result = (result < excludeLastRandNum) ? result : result + 1;
excludeLastRandNum = result;
return result;
}
测试:
void Update()
{
Debug.Log(RandomWithExclusion(0, 4));
}
最后一个数字永远不会出现在下一个函数调用中。
对于您的具体解决方案,只需替换
int index = Random.Range(0, 4);
和
int index = RandomWithExclusion(0, 4);
你需要做的是添加判断如果RandomColor returns 和上一个颜色相同,再调用一次,简单吧?
我有4种颜色。我想让玩家不能连续 2 次出现相同的颜色。当玩家与物体发生碰撞时,将调用 RandomColor()
。所以这个函数在游戏过程中被调用了很多次,有时玩家并没有改变他的颜色。
using UnityEngine;
public class ColorManager : MonoBehaviour {
public SpriteRenderer player;
public string playerColor;
public Color[] colors = new Color[4];
private void Awake()
{
RandomColor();
}
public void RandomColor()
{
int index = Random.Range(0, 4);
switch (index)
{
case 0:
player.color = colors[0]; //colors[0] is orange
playerColor = "orange";
break;
case 1:
player.color = colors[1]; //colors[1] is pink
playerColor = "pink";
break;
case 2:
player.color = colors[2]; //colors[2] is blue
playerColor = "blue";
break;
case 3:
player.color = colors[3]; //colors[3] is purple
playerColor = "purple";
break;
}
}
}
尝试使用 while 循环,执行 while 循环,但我显然做错了,因为有时我会连续两次收到相同的颜色。如果有人能弄明白并解释它会很好,那就太好了 how/why 因为我在这个问题上花了很多时间而且我很好奇。
首先,你需要一个可以生成带排除的随机数的函数。以下是我为此使用的内容:
int RandomWithExclusion(int min, int max, int exclusion)
{
int result = UnityEngine.Random.Range(min, max - 1);
return (result < exclusion) ? result : result + 1;
}
每次调用时,需要将结果保存在全局变量中,以便下次调用时传递给exclusion
参数。
我修改了该函数,这样您就不必在每次调用它时都这样做。新的 RandomWithExclusion
功能将为您做到这一点。
int excludeLastRandNum;
bool firstRun = true;
int RandomWithExclusion(int min, int max)
{
int result;
//Don't exclude if this is first run.
if (firstRun)
{
//Generate normal random number
result = UnityEngine.Random.Range(min, max);
excludeLastRandNum = result;
firstRun = false;
return result;
}
//Not first run, exclude last random number with -1 on the max
result = UnityEngine.Random.Range(min, max - 1);
//Apply +1 to the result to cancel out that -1 depending on the if statement
result = (result < excludeLastRandNum) ? result : result + 1;
excludeLastRandNum = result;
return result;
}
测试:
void Update()
{
Debug.Log(RandomWithExclusion(0, 4));
}
最后一个数字永远不会出现在下一个函数调用中。
对于您的具体解决方案,只需替换
int index = Random.Range(0, 4);
和
int index = RandomWithExclusion(0, 4);
你需要做的是添加判断如果RandomColor returns 和上一个颜色相同,再调用一次,简单吧?