n is the count of how many 2-second periods there have been. n%2 tells you whether n is odd or even. For the odd-numbered periods, you want t to go backwards (by subtracting time%dur from dur) otherwise, go forwards (just time%dur). time%dur gives you the remainder of of the time/dur operation, and is what generates the looping.
You should take a look at the noise() function. It gives you a Perlin noise field and will accept a 3-value array as its input parameter, so you could navigate the field using scaled x,y, and z position values and might also be able to incorporate time in some clever way.
Animating the position or scale shouldn’t affect sourceRectAtTime(). What will affect your result though, is the time parameter of toComp(). See if doing it this way helps:
The only thing I can think of would be to use sampleImage() (with postEffect=true) in a loop, starting at the top of the range and moving down (1 pixel at a time) until you find a pixel with non-zero alpha to determine the current height of that bar. Then use that value to drive the height of the mask.
BTW, the whole thing probably becomes much simpler if you can tap into whatever’s driving the mask expansion to also trigger the random positioning. For example, if the mask expansion is periodic, you could maybe use the same frequency value to trigger each new random position. That would be much more efficient and easier to code.