I figured out another solution, that helped me very much.
Explanation for my solution
Instead of using percentages, I suggest using integers. It is more readable.
In case of 3 items to spawn with own chances
Item A with a chance of 5 of 10 possibilities.
Item B with a chance of 3.
Item C with a chance of remaining 10 - 5 - 3 = 2 (chance).
- Fill their indices into a pool array or list with their amount of absolute chances.
In that example, it will produce a pool of indices like
[0,0,0,0,0,1,1,1,2,2] like 5 times Index zero, 3 times Index one, 2 times Index two.
I used this solution because I wanted to have a pool of chances, I want to select from.
So why not do it like that.
Now I shuffled that pool with indices.
At end I used a random index for that size of pool list.
In this example, item A has a chance of 5 to 10 to be selected, item B a chance of 3 and item C a chance of 2 to be selected.
public class Spawnable
public int chanceOf10 = 10;
public GameObject spawnPrefab;
public Spawnable creatableContent;
private Spawnable GetRandomSpawnable()
var pool = new List<int>();
int index = Random.Range(0, pool.Count - 1);
int spawnableIndex = pool[index];
Spawnable spawnablePrefab = creatableContent[spawnableIndex];
private void FillPool(Spawnable creatableContent, List<int> spawnablesIndices)
for (int indexOfSpawnable = 0; indexOfSpawnable < creatableContent.Length; indexOfSpawnable++)
Spawnable spawnable = creatableContent[indexOfSpawnable];
for (int j = 0; j < spawnable.chanceOf10; j++)
If required, my code for shuffle a list
public static class CSharpExtensions
private static readonly Random random = new Random(Guid.NewGuid().GetHashCode());
/// Puts each entry to another index
public static void Shuffle<T>(this IList<T> list)
for (int i = 0; i < list.Count; i++)
var newIndex = random.Next(0, list.Count - 1);
T a = list*;*
T b = list[newIndex];
list[newIndex] = a;
list = b;