Best way to randomly pick between two variables

I have this two variables.

Vector3 charpos1 = new Vector3(Random.Range(-2.3f, .50f), transform.position.y, transform.position.z);
Vector3 charpos2 = new Vector3(Random.Range(.50f, 2.3f), transform.position.y, transform.position.z);

What I’m planning to do is to choose between the two variables and instantiate it. Wha’ts the best way to do this do I have to add them to the list and use the random.range for its index number? Need your expert advice

Yes, the best way to do this is exactly that, add the vectors to a collection and then use a random function to extract one. So in this case something like this.

Vector3[] possiblePossitions = new Vector3[]
{
	new Vector3(Random.Range(-2.3f, .50f), transform.position.y, transform.position.z);
	new Vector3(Random.Range(.50f, 2.3f), transform.position.y, transform.position.z);
};

Vector3 chosenPosition = possiblePossitions[Random.Range(possiblePossitions.Length)];

Then you can add any number of vectors you want to that array and it will randomly select one.

Hi, you can try this:

private Vector3 charpos1 = new Vector3(x,y,z,);
private Vector3 charpos2 = new Vector3(x,y,z);
private List<Vector3> possiblePositions;

void Start(){
     possiblePositions.Add(charpos1);
     possiblePositions.Add(charpos2);
}

Vector3 ChooseRandom(){
     int randomPosition = Random.Range(0, possiblePosition.Count - 1);
     Vector3 positionChosen = possiblePositions[randomPosition];
     return positionChosen; 
}

In essence, what we do is first initialize the possible positions and then create a list. On start, we add the positions to the list. Then, whenever you call the ChooseRandom method, you will generate a random index between 0 and the maximum element of the list. Afterwards, that index will be used to get a Vector3 from the possibilities that you specified earlier, and return that Vector3.

Don’t forget to add at the top the System.Collections.Generic; namespace to be able to use the list.

The code is untested. Hope it helps!