0

enter image description hereLink 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" ? – Florian Claaßen Aug 7 '19 at 7:25
0

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 and bar2)

r = random(50);
[ 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]
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy