Repeat the same random positions of ellipses after n seconds

Hi there!

I’m workiung with the following code in p5js to make an animation of ellipses (raindrops). In the end I want to loop my video (screen recording). So the positions of the ellipses have to be in the same position after 8 sec as they are in the beginning… As the position of the ellipses are random, I want to repeat those random positions after 8 seconds. I tried to work with randomSeed(), but it did not work somehow.

code:
</>
let drops = []; // group of Drop

function setup() {
createCanvas(windowWidth, windowHeight);
//stroke(255);
noFill();
}

function draw() {
background(0, 0, 0, 20);

if(frameCount % 3 == 0) { //every 3th frame
	//drops.push(new Drop(mouseX, mouseY));
	drops.push(new Drop(random(width), random(height)));
}

for(d of drops) {
	d.display();
	d.grow();

}

}

//function mouseDragged() {
// drops.push(new Drop(mouseX, mouseY));
//}

class Drop {
constructor(xPos, yPos) { //initialize class
this.xPos = xPos;
this.yPos = yPos;
this.size = 0;
this.sizeMax = random(100, 200);
this.fc = 0; // custom framecount
this.stroke = 255;
}

display() {
	stroke(255);
	ellipse(this.xPos, this.yPos, this.size);
}

grow() {
	this.size = sin(this.fc * .015) * this.sizeMax;
	this.fc++; // grow by 1
	

	if(this.size >= this.sizeMax - 1) {
		this.remove();
	}
}

remove() {
	drops.splice(drops.indexOf(this), 1);
}

}
</>

I’d be very thankful if someone could help me :slight_smile:

Hello,

Please format your code as a courtesy to the community:
https://discourse.processing.org/faq#format-your-code

This worked for me:

  1. In setup():
randomSeed(99);  // You can change the seed to anything you want and save it to use again when you repeat!
  1. With a key press:
function keyPressed()
  {
  drops = [];       // Clears array of objects
  randomSeed(99);   // sets the same initial randomSeed for next cycle
  }

I will leave the timer for you… lots of examples out there.

Reference:
https://2ality.com/2012/12/clear-array.html
https://p5js.org/reference/#/p5/randomSeed

:)