[solved] Fill array with random integers without having any three in succession be the same

Hello everybody,

although filling an array with ints is rather easy I am stuck with the following problem:
I would like to fill an array with ints in a certain random range between for example 2-12 but I would also like to have not the same ints for any three successive indexes. So [4,5,4,8,11,5,10] would not be ok.

My current approach works to a certain extend but is not very ellegant and still fails sometimes for obvious reasons. As soon as my array holds more than two ints the if checks if any new int is the same as the two before the new one, if so it just does another Random.Range. Sure I could now just copy past that last if several times, but to my surprise I still sometimes get doublicates.

What am I missing?
Is there a more elegant sollution for a non programmer like me? :stuck_out_tongue:

void Start() {
for (int i = 0; i < tile_last; i++) {
            tile_stream[i] = Random.Range(2,tile_amount);

           if (i > 2) {
                int g = i - 2;
                int h = i - 1;
                tile_before_2 = tile_stream[g];
                tile_before_1 = tile_stream[h];
                tile_current = tile_stream[i];
                if (tile_before_2 == tile_current || tile_before_1 == tile_current) {
                    tile_stream[i] = (Random.Range(1, tile_amount));
                    print("random_2");
                }
                if (tile_before_2 == tile_current || tile_before_1 == tile_current) {
                    tile_stream[i] = (Random.Range(1, tile_amount));
                    print("random_3");
                }
                if (tile_before_2 == tile_current || tile_before_1 == tile_current) {
                    tile_stream[i] = (Random.Range(1, tile_amount));
                    print("random_4");
                }
            }
        }
}

You can just access the tiles directly in tile_stream, and use an inner loop instead of multiple ifs. So for example:

public static int[] GenerateRandomInts(int amount, int noRepeatZone, int rangeLow, int rangeHigh) {
    var output = new int[amount];
    bool repeats;

    for (int i = 0; i < amount; i++) {
        do {
            repeats = false;
            output[i] = Random.Range(rangeLow, rangeHigh);
            for (int j = i - 1; j >= 0 && j >= i - noRepeatZone; j--) {
                repeats = repeats || output[j] == output[i];
            }
        } while (repeats);
    }

    return output;
}

The j >= 0 on line 9 is why it doesn’t need to check if i > noRepeatZone.

1 Like

You’ll want to do a while loop

while(tile_stream[i] == tile_before_2 || tile_stream[i] == tile_before_1)
{
tile_stream[i] = Random.Range(2, tile_amount);
}

I could probably improve this more, but this is the basic idea.

1 Like

Thanks Errorsatz and Brathnann for your quick replies.
In the end I went with Brahtnann´s variant, because for me it is easier to read and understand and just three lines :stuck_out_tongue: Also it works like a charm.

void Start() {
for (int i = 0; i < tile_last; i++) {
            tile_stream[i] = Random.Range(2,tile_amount);
           if (i > 2) {
                int g = i - 2;
                int h = i - 1;
                tile_before_2 = tile_stream[g];
                tile_before_1 = tile_stream[h];
                tile_current = tile_stream[i];

                while (tile_stream[i] == tile_before_2 || tile_stream[i] == tile_before_1) {
                    tile_stream[i] = (Random.Range(1, tile_amount));
                }
            }
        }
}

I think there is a better solution for your problem

void Start()
    {
        int tile_amount = 12;
        int tile_last = 7;
        int[] tile_stream = new int[tile_last];
      
        List<int> tmpList = new List<int>();
        for (int i = 2; i < tile_amount+1; i++)
        {
            tmpList.Add(i);
        }

        tile_stream[0] = tmpList[Random.Range(0, tmpList.Count - 1)];
        tmpList.Remove(tile_stream[0]);
        tile_stream[1] = tmpList[Random.Range(0, tmpList.Count - 1)];
        tmpList.Remove(tile_stream[1]);
        tile_stream[2] = tmpList[Random.Range(0, tmpList.Count - 1)];

        tmpList.Add(tile_stream[0]);
        tmpList.Add(tile_stream[1]);

        for (int i = 3; i < tile_last; i++)
        {

            tmpList.Remove(tile_stream[i - 2]);
            tmpList.Remove(tile_stream[i - 1]);

            tile_stream[i] = tmpList[Random.Range(0, tmpList.Count - 1)];

            tmpList.Add(tile_stream[i - 2]);
            tmpList.Add(tile_stream[i - 1]);
        }
    }