# After Effects expressions to sum numbers and control charts_2

Link to Original Post/Question > After Effects expressions to sum numbers and control charts.

The answer featured in the original post was nearly the solution I needed for my specific problem. My question is this: Is there a way that I could get, say, four numbers that change randomly and constantly, yet equal 100 at all times?

For example, I was wanting bars in a bar graph to animate along with polling numbers that are still in flux. Let me know if I need to explain more.

I have followed the example in the previous post and have written the following expressions for ease of use:

Layer 1 [Text]

Source Text Parameter

``````val1 = Math.round(thisLayer.effect("Slider Control")("Slider"));

val2 = Math.round(thisLayer.effect("Slider Control 2")("Slider"));

t = val1 + val2;
val1 + " + " + val2 + " = " + t
``````

Layer 2 [Bar 1]

Scale Parameter

``````val1 = Math.round(thisComp.layer("Text").effect("Slider Control")("Slider"));

val2 = Math.round(thisComp.layer("Text").effect("Slider Control 2")("Slider"));

t = val1 + val2;
[ val1*100/t, 100 ]
``````

Layer 3 [Bar 2]

Scale Parameter

``````val1 = Math.round(thisComp.layer("Text").effect("Slider Control")("Slider"));

val2 = Math.round(thisComp.layer("Text").effect("Slider Control 2")("Slider"));

t = val1 + val2;

[ val2*100/t, 100 ]
``````
• Have you tried giving a random integer to number a and then determine number b by just "b = 100 - a" ? Commented Aug 7, 2019 at 7:25

If you use `random(50)` for the first three numbers the average value of their sum will be 75. So you can simply make the last number `100 - (num1 + num2 + num3)`.

However there is a chance that the three numbers could add to more than 100—up to 150 in the edge case that `random(50)` gives the result 50 for each one, thus summing to 150. So you could make the third number `random(Math.min(50, 100 - (num1 + num2)))`. This would mean that if the first two numbers added together were more than 50 it would limit the maximum value of the third number to equal or be less than enough to make the sum total 100. I'm not a statistician so I don't know if this would mathematically be perfectly random, but that usually doesn't matter for the purposes of motion graphics.

Which begs the question: why not just make all four numbers just random(50)? Over time the sum will tend towards exactly 100, it might just not add up to 100 at any one time.

But whatevs, the expression for scale would be:

Scale (bar1 )

``````r = random(100);
[ r, 100 ]
``````

Scale (bar2 )

``````r = random(100 - thisComp.layer("bar1").transform.scale[0]);
[ r, 100 ]
``````

Scale (bar3)

``````oneTwoTotal = thisComp.layer("bar1").transform.scale[0] + thisComp.layer("bar2").transform.scale[0];
r = random(Math.min(50, 100 - oneTwoTotal));
[r, 100]
``````

Scale (bar4)

``````r = 100 - (thisComp.layer("bar1").transform.scale[0] + thisComp.layer("bar2").transform.scale[0]  + thisComp.layer("bar3").transform.scale[0] );
[r, 100]
``````

Is there a way that I could get, say, four numbers that change randomly and constantly, yet equal 100 at all times?

Yes. The maths is below.

1. Create four random numbers.
2. Add the four random numbers (“random total”)
3. Divide each random by the total (giving us “normalised” numbers)

For example: four random numbers [0.83, 0.09, 0.95, 0.18] and the total: 2.05

Each divided by the total [0.405, 0.044, 0.463, 0.088]

Multiply each by 100, and add them – the sum of the four “normals” = 100.

Here’s one way you could apply this to a composition.

Start by adding a Null layer, and then:

1. Add four Slider effects, name them “randomA” to “randomD”
2. Option-click each slider and add the expression: `random()`
3. Add another Slider effect, name “randomTotal”, with expression that adds them: `effect("randomA")("Slider") + effect("randomB")("Slider") + effect("randomC")("Slider") + effect("randomD")("Slider");`
4. Add four more Slider effects, name “a” to “d”
5. Add expressions to each: `effect("randomA")("Slider") / effect("randomTotal")("Slider") * 100;` swapping “randomA” for corresponding randoms (“randomB”, “randomC” and “randomD”).

Below is a screenshot:

Then it’s just a matter of adding text layers that use the sliders “a”, “b”, “c” and “d”, as their source text with `Math.round()`. And bars that are scaled with the same inputs.