-2

I want to know logic over how to select any random numbers up to six between 0 to 8 where if (0,1) or (1,2) or (0,2) got select then the remaining one number out of 0 to 2 shall not be selected again same goes for 3 to 5 series if (3,5) or (3,4) or (4,5) got selected then remaining one number shall not be selected.

So for example in series 0 to 8 six number can be (0,1,3,5,6,8) or (1,2,4,5,7,8) or (0,1,3) or (4,6) etc

This points are kind of spawn points, so if all 0-2 points get fill then path will be blocked for player to move ahead.

Here is what I did, it kind of work but I think it can be improved for flexiblity and performance,

public static int[] getPointsToSpawn(int howManyToSpawn, int tillWhereToSpawn) {
    int[] howMany = new int[howManyToSpawn];
    List<int> randomCOllected = new List<int>();

    for (int i = 0; i < howManyToSpawn; i++) {

        int randomPoint = generateUniqueRandomNumber(0, tillWhereToSpawn, randomCOllected);
        randomCOllected.Add(randomPoint);

        if (randomCOllected.Contains(0) && randomCOllected.Contains(1) ||
                randomCOllected.Contains(0) && randomCOllected.Contains(2)
                || randomCOllected.Contains(1) && randomCOllected.Contains(2)
                ) {
            randomCOllected.Add(0);
            randomCOllected.Add(1);
            randomCOllected.Add(2);
        } else if (randomCOllected.Contains(3) && randomCOllected.Contains(4) ||
                randomCOllected.Contains(3) && randomCOllected.Contains(5)
                || randomCOllected.Contains(4) && randomCOllected.Contains(5)
                ) {
            randomCOllected.Add(3);
            randomCOllected.Add(4);
            randomCOllected.Add(5);
        } else if (randomCOllected.Contains(6) && randomCOllected.Contains(7) ||
                randomCOllected.Contains(7) && randomCOllected.Contains(8)
                || randomCOllected.Contains(6) && randomCOllected.Contains(8)
                ) {
            randomCOllected.Add(6);
            randomCOllected.Add(7);
            randomCOllected.Add(8);
        }
        howMany[i] = randomPoint;
    }
    return howMany;
}

Basically I don't want to generate more than 2 number between 0 to 2 or 3 to 5 or 6 to 8 range.

beresfordt
  • 5,088
  • 10
  • 35
  • 43
idurvesh
  • 644
  • 1
  • 7
  • 19

1 Answers1

0

It's pretty simple. You need three streps:

  1. Select two random numbers between 0 and 2
  2. Select two random numbers between 3 and 5
  3. Select two random numbers between 6 and 8

You can look at this post to see a code example: How do I generate a random int number in C#?

Community
  • 1
  • 1
swbandit
  • 1,986
  • 1
  • 26
  • 37