Complicated Question about Arrays and Duplicate Sorting Orders!

So I’m trying to spawn a random number, of randomly selected items from an array of prefabs. Then Place them in random positions with appropriate sorting order to their z position. It works okay, except on line 30, the random number is sometimes duplicated, this leads to two or more items with the same position and sorting order which causes weird visual for when they’re spawned.
Here is the full Code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditorInternal;
using System.Reflection;

public class Master : MonoBehaviour 
	public List<GameObject> allPrefabs = new List<GameObject> ();
	List<GameObject> spawnList = new List<GameObject> ();
	List<int> number = new List<int> ();
	int z;

	void Start () 
		//Take the list of Prefabs and shuffle it
		for (int r=0; r<allPrefabs.Count; r++) 
			GameObject temp = allPrefabs[r];
			int randomIndex = Random.Range(r, allPrefabs.Count);
			allPrefabs[r] = allPrefabs[randomIndex];
			allPrefabs[randomIndex] = temp;
		//Select a random amount of items to spawn, spawn each prefab, add it to a new Array for future actions
		int i = Random.Range (2,allPrefabs.Count);
		for (int n=0; n<i; n++) 
			GameObject temp = allPrefabs[n];
			z = Random.Range(-3,4);
			Renderer renderer = temp.GetComponent <SpriteRenderer>();
			renderer.sortingOrder = -z;
			GameObject currentSpawn = Instantiate(temp,new Vector3(Random.Range (-2,2),-1,z) , Quaternion.identity) as GameObject;
			spawnList.Add (temp);


	void Update () 
//Need to make sure that a sortingOrder is used only once to prevent weird placement

Now the trick is, I’m limited on z space, as the rooms that these objects are spawning in are limited to the -3 and 4 z axis BUT I want to spawn as many as 20 to 30 objects So I need a big range of sorting orders that will also correlate with each items z position to each other. Is there a fix that anyone can think of?

You could maintain an array with previously used z positions and compare them all to the current random number to see if any previous random numbers were the same. You just set up a loop that will run as long as the randomly generated number is the same.

Generally, setting up a loop where you don’t know a concrete end is coming is bad policy, but the odds are insanely against generating the same random number enough to create a loop that repeats multiple times.