# Spawn chance, modulated by probability weighted by progress

I've been struggling with this for over a day now. What seemed a simple task, has just wrecked me. I hope someone here can help me figure out what I'm missing. Please, please help me :)

The functionality I need:

Think of my game as Guitar Hero, but instead of pre-placed nodes, I place the nodes randomly as the game progresses, at certain valid points in time. Now, instead of relying on randomness alone, I've made phases of difficulty, where I set a certain number of nodes to be spawned throughout the phase, and I'd like to have those evenly distributed within the phase. Since it's an arcade/highscore game, it needs to be roughly the same number of nodes placed every time you play the game, but the nodes should be placed differently each time. Until recently, I simply had something like this code, called for each valid point in time:

if(Random.Range(0f, 1f) < 0.25)
// Spawn node


The problem:

Now I want to balance the number of nodes per phase. To do this, I have these variables:

• TotalNodesToPut
• NodesPut
• NodesLeftToPut
• PhaseProgress (0.0-1.0 normalized percentage of progress)

My plan was, to have the base chance of spawning a node to be 50%. I would then like to modulate that down to 0% and up to 100% depending on how many nodes still need to be put down, and how far we have progressed through the phase. It should be so that the percentage of nodes put down follows the progress percentage. If we have spawned 5 of 20 nodes (25%) but have progressed 75% of the phase, the chance to spawn a node should be really high. If we have spawned 15 of 20 nodes (75%) but have only progressed 25% of the phase, the chance to spawn a node should be really low.

It seems like a simple task, but I just can't get it to work :( I've tried dozens of things, started over again and again, and it's just never right, which is why I'm hesitant to show any code. I wouldn't want to start people off on the wrong foot. I have tried searching on the web, but haven't found anything about this particular problem. Any help is much appreciated!

NodeRate = NodesLeftToPut / TimeLeft;
ChanceToSpawnNode = TimeStep * NodeRate
if (Random.Range(0f, 1f) < ChanceToSpawnNode) { Spawn(); }


The NodeRate is the number of nodes per second that you expect to occur, which changes as the timer drops and also when new nodes spawn.

The chance to spawn during a time step is based on the NodeRate and the time delta TimeStep.

I think this fits the approach that you are asking for, but I can see that it will have problems if the spawn rate is too high or too low, but maybe that fits within your game.

Another suggestion which might work... say you have 100 'frames' in the level and during each frame either spawn a node or not. Say you have 20 nodes you want to spawn in the level. Make an array with the first 20 elements to spawn a node, the next 80 elements say don't spawn. Then shuffle the array. During the level, check whether to spawn a node at this time step or not. This might help if you are able to calculate levels ahead of time.

• Thanks for your answer! While the array thing would work, it's not very dynamic, and we'd like to be able to react to certain things in the game logic during the game, and add more points at certain times. Your first suggestion is closer to what we wanted, and it's close to what we ended up with, but we wanted to stay away from using time as a determinant in the calculation. Thanks again! – Ultroman the Tacoman Dec 16 '18 at 0:53

We finally figured it out, and it WAS a super simple problem, which I was over-complicating, and I was missing the fact that I had to account for the difference being above or below 0.

This swings between 0 and 1, and you can set whatever base chance you want, for when pointsPut is the same as estimatedPointsPut.

var baseChance = 0.3f;
if (pointsPut <= estimatedPointsPut)
{
modularChance = 1 - ((float) pointsPut / estimatedPointsPut);
}
else
{
modularChance = ((float) estimatedPointsPut / pointsPut) - 1f;
}
modularChance *= 1 - baseChance;
modularChance += baseChance;