# Select random number in series

I want to know logic over how to select six random numbers 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) etc

To do this you need to track that over time. So some sort of collection will be needed.

Easiest route would be a List of the range of values. Randomly grab a value from the List, removing it in the process. When the list is empty, the sequence is complete.

//create sequence
var lst = new List<int>(9);
for(int i = 0; i < 9; i++)
{
}

//extract value
int i = Random.Range(0, lst.Count);
int result = lst[i];
lst.RemoveAt(i);
1 Like

Wow that seems to be new approach, This numbers are kind of spawn points, so if all 0-2 points get filled then path will be blocked for player to move ahead. How would I know and avoid with your way that 0 to 2 ( 3 to 5,6 to 8) all numbers has got generated?

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

Here is what I hard codedâ€¦

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

if(randomCOllected.Contains(0) && randomCOllected.Contains(1) ||
randomCOllected.Contains(0) && randomCOllected.Contains(2)
|| randomCOllected.Contains(1) && randomCOllected.Contains(2)
)
{

}
else if (randomCOllected.Contains(3) && randomCOllected.Contains(4) ||
randomCOllected.Contains(3) && randomCOllected.Contains(5)
|| randomCOllected.Contains(4) && randomCOllected.Contains(5)
)
{

}
else if (randomCOllected.Contains(6) && randomCOllected.Contains(7) ||
randomCOllected.Contains(7) && randomCOllected.Contains(8)
|| randomCOllected.Contains(6) && randomCOllected.Contains(8)
)
{

}

howMany[i] = randomPoint;

}
return howMany;

}

so you want to guarantee a gap? simple, 3 ranges in a similar fashion as above. Randomly pick one from each range, as above. Donâ€™t do anything with that number, just remove it.
Combine the three ranges and do as above to select the ones you want to use.

Remove 2 random values from the list (1â€¦8):

List<int> result = Enumerable.Range(1, 8).ToList();
result.RemoveAt(Random.Range(0, result.Count));
result.RemoveAt(Random.Range(0, result.Count));

Ok, you dont want to have something like (0, 1, 2)
so:

List<int> result = Enumerable.Range(0, 9).ToList();
result.RemoveAt(Random.Range(0, 2));
result.RemoveAt(Random.Range(2, 4));

Also, please note that if you select 6 in 8, and will not allow (0, 1, 2) or (3, 4, 5), then 6, 7 and 8 must be always present.

@L-Tyrosine not seen Enumerable.Range before, did some googling; do you need System.Linq namespace for that or is there another one?

Sorry, I was wrong, there are 9 numbers (not 8).
So:

// Generate (0, 1, 2, 3, 4, 5, 6, 7, 8) list
List<int> result = Enumerable.Range(0, 9).ToList();

// Remove something in 0..2 range
result.RemoveAt(Random.Range(0, 2));

// Remove something in 3..5 range
result.RemoveAt(Random.Range(2, 4));

// Remove one more random value (now anything goes)
result.RemoveAt(Random.Range(0, result.Count));

This is problem that I would expect to see in Google Jam =D

1 Like

youâ€™d think theyâ€™d start with collab on making a more interesting webpage for it :p:smile:

Thank you all for support â€¦WIth above code if I increase range from 0 -8 to 0-14 repetition will get increase, so I end up with following code,

public static int[] getPointsToSpawn(int howManyToSpawn, int tillWhereToSpawn,int howManyToChangeLane)
{

int[] howMany = new int[howManyToSpawn];

List<int> randomCOllected = new List<int>();

for(int k = 3; k <= tillWhereToSpawn; k += 3)
{
randomCOllected.Add(provideRnd(k - 3, k));
}

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

int randomPoint = generateUnique(0, tillWhereToSpawn, randomCOllected);

howMany[i] = randomPoint;

}
return howMany;

}

I pre added ignore values to collected random list,so new random will never spawn that numberâ€¦Thank you again for your responsesâ€¦

@L-Tyrosine Glad you liked this