I have a list of int values with random numbers from 1 to 11, Point is, I have to compare the first and second value in the list if one is higher or lower. Although, sometimes same values show up, like 5-5 or 10-10, which I don’t want to happen. I don’t want two values two show up one after another.
This is the code that I tried, could I get some directions ?
I don’t quite get your problem
Do you hace a problem with this:
1 4 6 3 6 2 8 7
Or this:
1 3 3 6 3 5 7 3 6 7 6 7 7
The first one should be easy to fix, using Linq (which is already in use by default) you can get a list with only unique elements if you use myList = myList.Distinct.ToList();
The second one is a little bit more complicated
int next = myList[myList.Count-1]; //this will be used to cache the value we need
for (int i = myList.Count - 2; i < 0; i++) { //Looping trough backwards
if (next == myList[i]) //if they are the same
myList.RemoveAt(i+1); //remove them
next = myList[i]; //swap next with current
}
I’m going trough the list backwards, because then I can safely remove items from it without causing errors
not efficient, I’ll be the first to admit, but as long as it is only going to be run once or once every game, should be fine.
//at top of file
using System.Collections.Generic;
//in script
int maxValue = 11;
int optionsNum = 20;
int[] numArray;
numArray = new int[optionsNum];
for(int i = 0; i < optionsNum; i++){
List<int> tempOptions = new List<int>();//create list of options
for(int j = 0; j < maxValue; j++){
if(i > 0 && j != optionsNum[i-1])//check if previous and current are different
tempOptions.Add(j);//populate list options
}
int chosen = UnityEngine.Random.Range(0,tempOptions.Count);//choose an option
numArray[i] = tempOptions[chosen] + 1;//add option, +1 cause started with 0 instead of 1
}