I have tried this code to iterate an array in a circular way and assign the y values to five circles. But I suspect it is not the most efficient way. Are there other ways to do it?

int k=0,km = 5;
void setup() {
size(300, 100);
frameRate(5);
println("use: mouseX and mouseY");
}
void draw() {
background(200);
fill(255, 0, 128);
noStroke();
for (int i = 0; i < 10; i++) circle(40+i*25,50+make_k(),10);
}
int make_k() {
k += km;
if ( k > mouseX/6) km= -mouseY/10;
if ( k <-mouseX/6 ) km = mouseY/10;
return k;
}

I needed a spreadsheet to understand what was happening. Converts continuous values given by mouseX and mouseY into discrete values, and change sign periodically. Amazing! I liked it a lot! I did not understand why sometimes the balls stopped, but it seems that it is because the series of data of a cycle coincides with the number of balls, therefore it reassigns the same values to the same balls.