mercredi 20 novembre 2019

Non-consecutively repeating array of integers

I am trying to generate a non-consecutively repeating array of integers. What I have so far works pretty well except that it will occasionally generate a set of double 0's. My questions are:

  1. How can I stop that
  2. There has to be a better way to do this right? It's pretty ugly.

I am grateful for any insights you can share. Thank you!

This is the code:

        int[] randInts = new int[20];

        Random rand = new Random();


        for (int i = 0; i < 20; i++)
        {
            randInts[i] = rand.Next(0, 8);
        }

        for (int i = 1; i < 20; i++)
        {
            if (randInts[i] == randInts[i - 1] && randInts[i] < 8 && randInts[i] > 0)
            {
                randInts[i]--;
            }

            else if (randInts[i] == randInts[i - 1] && randInts[i] > 0)
            {
                randInts[i]++;
            }
        }



Aucun commentaire:

Enregistrer un commentaire