问题
I wrote a program in Processing 3.5.4. It's basic structure is as follows:
int SOMEINITIALSTUFF;
Class[] classArrays = new Class[];
void setup() {
Sets up the simulation to run;
size(1200, 700);
}
void draw() {
background(255, 200, 200);
Runs Simulation;
Collects data;
}
This runs fine. What I would like to do is run this program multiple times to gather some statistics.
I can't figure out how to do this. I want to essentially put this whole code into a loop, and collect the data it creates for each iteration, possibly running it thousands of times. I've tried just that, but it breaks the program. Any suggestions?
回答1:
You can do this with a couple simple steps, some of which may require some refactoring:
- Determine the conditions which define the end of the simulation.
- Set un a method which will run only once the simulation is done. We'll call it
Reset()
to make things easier. - In
Reset()
, make sure that you reinitialize any global variables which is initialized at their creation. Set them back to their initial value. - In
Reset()
, runsetup()
. - Let nature follows it's course, your application has been tricked into beginning anew.
Of course, you may want to organize your code so the information you gather won't be erased, whether by saving it to a different file/appending it to a file at every time you run a new simulation, or by preserving it in a global variable which won't be reset. I don't have enough details to elaborate on this front, but I think you'll understand the idea I'm advancing.
Have fun!
来源:https://stackoverflow.com/questions/60750890/how-to-iterate-the-same-sketch-multiple-times-processing