- Home /
Prevent items from being listed more than 3 times in a row after shuffle
I have a list of two Vector3 locations (NorthPos and SouthPos). These locations repeat several times on the list. For instance, if the list has 10 elements, half of the elements will be NorthPos and the other half will be SouthPos. I am able to shuffle the elements using the Fisher–Yates shuffle. However, I want to prevent the locations from repeating more than 3 times in a row (e.g. SouthPos, NorthPos, NorthPos, NorthPos, NorthPos, SouthPos, etc.). What's the best way to do this in C#?
//add items to list
for (int i = 0; i < series1int / 2; ++i) {
StartSeqAcquisition.Add (NorthPos);
}
for (int i = 0; i < series1int / 2; ++i) {
StartSeqAcquisition.Add (SouthPos);
}
//shuffle items for (int t = 0; t < StartSeqAcquisition.Count; t++ )
{
Vector3 tmp = StartSeqAcquisition[t];
int randomIndex = Random.Range(t, StartSeqAcquisition.Count);
StartSeqAcquisition[t] = StartSeqAcquisition[randomIndex];
StartSeqAcquisition[randomIndex] = tmp;
}
Answer by unidad2pete · Aug 11, 2017 at 05:30 PM
You can add a new int to count when you add a new value:
int countNorth = 0;
int countSouth = 0;
for (int i = 0; i < series1int / 2; ++i)
{
if(countNorth < 3)
{
countNorth++;
StartSeqAcquisition.Add(NorthPos);
}
}
for (int i = 0; i < series1int / 2; ++i)
{
if (countSouth < 3)
{
countSouth++;
StartSeqAcquisition.Add(SouthPos);
}
}
I mistakenly forgot to add the shuffle code. I just added it above.
The code you sent looks as though it would prevent more than 3 repeats of an item. What I would like to prevent is an item being listed more than 3 times in a row.
Thanks in advance for your help!
If you can share all code we can help you better, I dont understand exactly what do you want. What means a row? I supose StartSeqAcquisition is a List of int, there are not rows in a list.
Do you mean, not repeating when you shuffle the items?
The important part of the code is above. The entire script is too large and convoluted to put here.
Let's say I have the following list:
NorthPos NorthPos NorthPos NorthPos NorthPos SouthPos SouthPos SouthPos SouthPos SouthPos
After shuffling the list, I get this:
SouthPos SouthPos NorthPos NorthPos NorthPos NorthPos SouthPos NorthPos SouthPos SouthPos
In this example, NorthPos is listed more than 3 times in a row. It is listed 4 times in a row (see Bold). I want to be able to prevent this kind of thing from happening.
Thanks again for your help!
Ok, its not perfect, but It's the only thing that occurs to me.
// I make new list to shuffle, more easy to check with relative numbers
public List<int> list;
private void Start()
{
list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
}
// Supose 0-4 are North of your list, and 5-9 are South
// for my test, when I press space, my int list get shuffle until there are not 3 relative numbers.
private void Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
bool ok = false;
while (!ok)
{
ok = true;
for (int i = 0; i < list.Count; i++)
{
if (i + 2 < list.Count && i - 2 >= 0)
{
if (list[i] + 1 == list[i + 1] || list[i] - 1 == list[i - 1] || list[i] + 1 == list[i + 2] || list[i] - 1 == list[i - 2])
{
if (list[i] + 2 == list[i + 2] || list[i] - 1 == list[i - 2])
{
int tmp = list[i];
int randomIndex = Random.Range(i, list.Count);
list[i] = list[randomIndex];
list[randomIndex] = tmp;
ok = false;
break;
}
}
}
}
print(list[0]+""+ list[1] + "" + list[2] + "" + list[3] + "" + list[4] + "" + list[5] + "" + list[6] + "" + list[7] + "" + list[8] + "" + list[9]);
}
}
}
// Now, I have a list with shuffle like you want.
// I make a copy of your list and clear the list.
List<Vector3> tempList = StartSeqAcquisition;
StartSeqAcquisition.Clear();
// Now, add items with int list order
for (int i = 0; i < list.Count; i++)
{
StartSeqAcquisition.Add(tempList[list[i]]);
}
Sorry, I know its not perfect, but try it
2 first and 2 last numbers are not changed , to prevent null ranks, but its easy to fix, if you cant, tell me.
Your answer
![](https://koobas.hobune.stream/wayback/20220612132513im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can i shuffle a list 5 Answers
Only want to change one item in list and not all of it's duplicates? 1 Answer
Adding object to list add the same object to another list 1 Answer
Why does the script don't apply to the other duplicated player 0 Answers
How to make a list store and save values even while changing scenes. 1 Answer