Spawn Settings?

I want to create a function that spawns NPCs based on an adjustable percent. For example, let’s say we have 3 types of monsters, A,B, and C. Before starting the game, the player can choose to set their spawn rates. By default, A, B, and C each have a 33.33…% chance to generate. If the player wants to spawn zero monster Cs, then the ratio will change to a 50-50 split for monsters A and B. Hopefully that part is clear, because the next part is where I am struggling.

I have three variables established for each monster’s spawn frequency. I have the system in place so that these all adjust when the player changes one of the settings. How would I go about creating a function that looks at each spawn frequency variable and generating a monster based on their respective frequencies? I thought about using if statements like If (random number) <=33.33…, spawn type A, if (random number) <= 66.66… and >=33.33… spawn type B, etc.

I am more so looking for advice on the logical structure, since I am most likely making this more complicated than necessary. Any help would be appreciated. Thanks!

I think im following what your looking for,

should be able to run a sequence with loops. take the output of total monsters to spawn * the % of each monster( floored to round) then plug each result into a loop to set the number of times it would run that loop, and have each loop spawn one monster…

so if your total to spawn was 10, for 33.33 % you would get 3 monsters of type A, 3 of type B, and 3 of type C

depending on how the frequencies line up and rounding it may end up spawning less total monsters than intended so you would probably need to do a check against total and if its less then total number intended spawn an extra.

for this you could have a variable that adds 1 to it every monster that spawns and check against the total you want to spawn if its less, you could spawn 1 random to make up for the missing one

You did the complicated part :). If you want more than 3 monsters, you could store their spawn rate inside an array and do something like that (to spawn one monster based on its spawn rate) :