# 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:

``````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%, and then 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. So I thought this would do the trick:

``````if(Random.Range(0f, 1f) < 0.5f + ((PhaseProgress - (NodesLeftToPut / TotalNodesToPut)) - 0.5f))
``````

But, to take a simple example, where we’re halfway through the phase, and have placed half the nodes, so we should end up with 0.5:

``````0.5f + ((0.5 - (10/20)) - 0.5f) = 0
``````

This is only one of the hundreds of different ways I’ve tried and failed. I thought I had it dozens of times, but then something went wrong at the end or beginning of the phase, or the modulation percentage was way too low or something.

It seems like a simple task, but I just can’t get it to work I have tried searching on the web, but haven’t found anything about this particular problem. Any help is much appreciated!

Still trying to wrap my head around this, but the math in your example doesn’t really make sense.

``````>PhaseProgress == NodesLeftToPut/TotalNodesToPut So,

PhaseProgress - (NodesLeftToPut/TotalNodesToPut) == PhaseProgress - PhaseProgress == 0

> 0.5f + (PhaseProgress - (NodesLeftToPut/TotalNodesToPut)) - .5f

0.5f + (PhaseProgress - (NodesLeftToPut/TotalNodesToPut)) - .5f

0.5f + (0) - .5f = 0
``````

No matter what, you’d end up with 0.

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;
``````