[Help] Obstacle Spawner Efficiency

Apologies for the seemingly easy question, i am new to coding and Unity

I Have some objects being procedurally generated via a SpawnObstacles() Function, It works, however the last part is incredibly inefficient (See Below) :
** **void SpawnObstacles () { int randomIndex = Random.Range(0, spawnPoints.Length); for (int i = 0; i < spawnPoints.Length; i++) { if (randomIndex != i) { float XAxis = spawnPoints[i].position.x; float YAxis = spawnPoints[i].position.y; float ZAxis = spawnPoints[i].position.z; Vector3 spawn = new Vector3(XAxis, YAxis, ZAxis + Random.Range(0f,15f)); Instantiate(obstacle, spawn, Quaternion.identity); } if(randomIndex == Random.Range(0, spawnPoints.Length)||randomIndex == Random.Range(0, spawnPoints.Length)) { // Do nothing } } }** **
Essentially, in this code i spawn my obstacle whenever the current iteration of i does not equal randomIndex.
This however means there is 7 obstacles currently being spawned out of a possible 8. I want there to be a random number of obstacles spawned, not 7.
(Hence the incredibly inefficient code shown after the if(randomIndex != i), How do i go about making this more efficient?

Here is some simple code to do what you want.

int rndAmount = Random.Range(1, 8);
for (int i = 0; i < rndAmount; i++)
{
    Vector3 spawn = spawnPoints[i];
    spawn.z += Random.Range(0f, 15f);
    Instantiate(obstacle, spawn, Quaternion.identity);
}

Not tested at all, but I don’t see why it wouldn’t work.