#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# How do I store a random variable while looping?

edited January 2015

Hello, this is my first post and I am new to programming.

In order to learn processing I am trying to make a drawing program that shows a randomly generated line and curve once a second, then when the user clicks on the screen the line and curve that are on the screen at that time stay on the screen, as in they are redrawn every frame, whilst the program continues to make a single random line and curve every second.

Essentially the program will "propose" a line and curve every second and the user will either accept by clicking or reject by ignoring, thus resulting in a collaborative drawing between the program and user.

This is my code so far:

``````float x1;
float x2;
float y1;
float y2;
float cx1;
float cx2;
float cx3;
float cx4;
float cy1;
float cy2;
float cy3;
float cy4;

int clicks=0;

void setup() {
size(500, 500);
frameRate(1);
}

void draw() {

background(100, 100, 100);

x1 = random(500);
x2 = random(500);
y1 = random(500);
y2 = random(500);

cx1 = random(500);
cx2 = random(500);
cx3 = random(500);
cx4 = random(500);
cy1 = random(500);
cy2 = random(500);
cy3 = random(500);
cy4 = random(500);

line(x1, x2, y1, y2);
curve(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);

if (clicks == 1) {
line(x1, x2, y1, y2);
curve(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
}
}

void mousePressed() {
clicks = clicks+1;
}
``````

It runs, however the variables which define the location of the line and curve when clicks == 1 change every second when redrawn, how do I store them so that when the user clicks in the window the location of the line and the curve at that time is redrawn until the program stops?

Tagged:

• edited February 2015

Well, simply stop picking random() coordinates once user clicks: *-:)

``````// forum.processing.org/two/discussion/9152/
// how-do-i-store-a-random-variable-while-looping

// 2015-Jan-24

int lx1, lx2, ly1, ly2;
int cx1, cx2, cx3, cx4;
int cy1, cy2, cy3, cy4;

boolean clicked;

void setup() {
size(500, 500);
frameRate(60);
smooth(4);

stroke(0);
strokeWeight(2);
fill(#FFFF00);

// Registers a post-draw method so the frame is shown and awaits
// some seconds for the user to decide whether to click at it:
registerMethod("post", this);
}

void post() {
delay(2000); // gives 2 seconds for the user to decide.
dequeueEvents(); // forces immediate callback of input events like mousePressed().
}

void mousePressed() {
clicked ^= true; // toggles clicked state.
}

void draw() {
frame.setTitle(clicked? "Frozen!" : "Running...");

if (!clicked) {
lx1 = (int) random(width);
lx2 = (int) random(width);
ly1 = (int) random(height);
ly2 = (int) random(height);

cx1 = (int) random(width);
cx2 = (int) random(width);
cx3 = (int) random(width);
cx4 = (int) random(width);

cy1 = (int) random(height);
cy2 = (int) random(height);
cy3 = (int) random(height);
cy4 = (int) random(height);
}

background(0200);
line(lx1, lx2, ly1, ly2);
curve(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
}
``````
• edited February 2015

A more simplified version w/o `registerMethod("post", this);` nor forced delay(). \m/
However, undocumented dequeueEvents() is still needed before checking for clicked: :-<

``````// forum.processing.org/two/discussion/9152/
// how-do-i-store-a-random-variable-while-looping

// 2015-Jan-24

int lx1, lx2, ly1, ly2;
int cx1, cx2, cx3, cx4;
int cy1, cy2, cy3, cy4;

boolean clicked;

void setup() {
size(500, 500);
frameRate(.5); // gives 2 seconds for the user to decide.
smooth(4);

stroke(0);
strokeWeight(2);
fill(#FFFF00);
}

void mousePressed() {
// Toggles clicked state and saves a frame shot when it's true:
if (clicked ^= true)  saveFrame(dataPath("####.jpg"));
}

void draw() {
dequeueEvents(); // forces immediate callback of input events like mousePressed().
frame.setTitle(clicked? "Frozen!" : "Running...");

if (!clicked) {
lx1 = (int) random(width);
lx2 = (int) random(width);
ly1 = (int) random(height);
ly2 = (int) random(height);

cx1 = (int) random(width);
cx2 = (int) random(width);
cx3 = (int) random(width);
cx4 = (int) random(width);

cy1 = (int) random(height);
cy2 = (int) random(height);
cy3 = (int) random(height);
cy4 = (int) random(height);
}

background(0200);
line(lx1, lx2, ly1, ly2);
curve(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
}
``````
• Please ignore GoToLoop's replies... I don't think he's grasped exactly what it is you're asking, and his replies aren't exactly helpful to you.

There's a lot of changes here. I've moved you to an object-based model. Classes are blueprints for objects. i've also included two ArrayLists, which are like arrays, except you can dynamically add things to them. I do this when the user clicks. I also draw all the ones that are in the ArrayLists. Finally, I've done away with your framerate call, and moved you to a time-base updating system, so that the new prospective line and curve are updated each second.

``````class LineObj {
float x1, x2, y1, y2;
LineObj() {
x1 = random(500);
x2 = random(500);
y1 = random(500);
y2 = random(500);
}
void draw() {
line(x1, x2, y1, y2);
}
}

class CurveObj {
float cx1, cx2, cx3, cx4;
float cy1, cy2, cy3, cy4;
CurveObj() {
cx1 = random(500);
cx2 = random(500);
cx3 = random(500);
cx4 = random(500);
cy1 = random(500);
cy2 = random(500);
cy3 = random(500);
cy4 = random(500);
}
void draw() {
curve(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
}
}

ArrayList<LineObj> lines = new ArrayList();
ArrayList<CurveObj> curves = new ArrayList();

LineObj newLine = new LineObj();
CurveObj newCurve = new CurveObj();

int time;

void setup() {
size(500, 500);
time = millis() + 1000;
}

void draw() {
background(100, 100, 100);
for (int i=0; i<lines.size (); i++) {
lines.get(i).draw();
}
for (int i=0; i<curves.size (); i++) {
curves.get(i).draw();
}
if (millis() > time ) {
newLine = new LineObj();
newCurve = new CurveObj();
time = millis() + 1000;
}
newLine.draw();
newCurve.draw();
}

void mousePressed() {
}
``````
• Ah, thank you both!

Any replies are helpful to me learning, sometimes it is very hard to put these things into words so I apologize to GoToLoop if I was not clear about my intentions, I'll still go over all three replies in detail to learn what I can so that GoToLoops contributions don't go to waste.

TfGuy44 your code does exactly what I wanted! now I'll open it and my original sketch and spend time modifying my original myself (or just rewrite it from scratch) so that I learn exactly what you did.

Looks like I'll be reading a lot about objects and classes as well.

Thanks again, long live processing!