We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › a question about syntax...
Page Index Toggle Pages: 1
a question about syntax... (Read 507 times)
a question about syntax...
Nov 16th, 2007, 1:25pm
 
Hi, this is a typically newbie question: I'd like my sketch to choose two numbers randomly between 0 and 100 and I absolutely need these two numbers to be at least 20 units apart. How do I ask the program to regenerate the numbers if they don't respect the rules? My first idea was to put some kind of "label/goto" flags to force the program to jump one step back (as I use to do on my old casio pocket calculator!!) but I don't think this is possible here... Thank you.

D.S
Re: a question about syntax...
Reply #1 - Nov 16th, 2007, 4:03pm
 
Here's one way of doing it.
in a "while" statement, it runs the instructions until the condition is true.

Look at the output window to see the results. Click to regenerate.

Code:

int value01, value02;

void setup() {
size(150,150);
background(0);
noLoop();
}

void draw() {
boolean condition = false;
while(!condition) {
value01 = int(random(0,100));
value02 = int(random(0,100));

if ((value01-value02 > 20) || (value02-value01 > 20)) {
println("Value 01: " + value01 + " -- Value 02: " + value02);
condition = true;
}
else {
println("FAILED >> Value 01: " + value01 + " -- Value 02: " + value02);
}
}
}

void mousePressed() {
redraw();
}


Another way of doing it, would be to pick a first random value, and then pick the second value based on the first one (value01 +/- 20). But this way, you'll need some conditions to check if you need to add or subtract to the value in order to not go over or below the limits (100 and 0).

Cheers!
Page Index Toggle Pages: 1