It's for counting!? I always had this idea it was like a pseudo-random way of picking one option out of several, like in my part of México we (used-to) use (not-really Spanish) "de tín marín de do-pingüé, cúcara mácara tîtiri füé, yo no fuí, fué te-té, pégale, pégale que ese mero fué". Which reminds me of another one I learnt from a book as a child but have never heard it from anyone else out in the open: "pin-pin, zarambolín, el ratón comer comer, el ratón comer comer, ¿cuánto das por el ratón? cien tostones y un doblón, arre mula carretera pin-pin estás afuera!".