Generate random number each time and don't inc

2019-02-27 19:08发布

问题:

I have 4 colors. I want to make it so that the player can't be the same color 2 times in a row. When a player collides with an object, the RandomColor() is called. So this function is called many times during the game and sometimes the player does not change his color.

 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;
             }    
     }    
 }

Tried using while loop, do while loop, but I'm obviously doing it wrong, since I receive the same color twice in a row sometimes. It would be great if anyone figures it out and explains how/why it works, because I spent a big chunk of time on the issue and I am very curious.

回答1:

First, you need a function that can generate a random number with exclusion. Below is what I use for that:

int RandomWithExclusion(int min, int max, int exclusion)
{
    int result = UnityEngine.Random.Range(min, max - 1);
    return (result < exclusion) ? result : result + 1;
}

Each time you call it, you need to store the result in a global variable so that you will pass that to the exclusion parameter next time you call it again.

I modified the function so that you don't have to do that each time it is called. The new RandomWithExclusion function will do that for you.

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;
}

Test:

void Update()
{
    Debug.Log(RandomWithExclusion(0, 4));
}

The last number will never appear in the next function call.

For your specific solution, simply replace

int index = Random.Range(0, 4);

with

int index = RandomWithExclusion(0, 4);


回答2:

What you need to do is add a judgement if the RandomColor returns a same color as prev one, just call it again, easy right?