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 › why is the random() method not static
Page Index Toggle Pages: 1
why is the random() method not static? (Read 732 times)
why is the random() method not static?
May 11th, 2009, 4:07pm
 
Just a quick question: why are the random methods in PApplet not static? It's quite annoying to be able to use static imports for cos(), ceil(), and every other mathematic function, but not for random().
Re: why is the random() method not static?
Reply #1 - May 11th, 2009, 5:00pm
 
You can use http://processing.org/reference/randomSeed_.html
randomSeed. Thats pretty useful. You can combine it and also create a random randomseed and print it out. so you get random numbers each time you start it but could recreate them if you want to.

Re: why is the random() method not static?
Reply #2 - May 11th, 2009, 10:43pm
 
Funnily, I knew about static import but haven't used it yet.
Somehow, Processing does a primitive (pre-Java 1.5) static import by using a pre-processor to repeat the definitions of some functions in sub-classes, to expose them as simple function in PApplet. [EDIT] Actually not really, since the functions are not static, but that's the same spirit, to expose methods as simple functions.

Your remark makes me think you use Processing with Eclipse, not within the PDE, since these functions are accessible without prefix in regular sketches...
The reason is simple: unlike other mathematical functions, random() isn't a self-sufficient function, it depends on members, at least the random seed. So it cannot be static.
You can still use Java's one, no
Re: why is the random() method not static?
Reply #3 - May 12th, 2009, 2:42am
 
Did i get the question wrong?
Re: why is the random() method not static?
Reply #4 - May 12th, 2009, 3:29am
 
I fear so...  Roll Eyes
Although you put your finger on the problem with the randomSeed reference!
Re: why is the random() method not static?
Reply #5 - May 12th, 2009, 3:36am
 
Yeah, thats what i thought when i read your answer...

Ahh, now i got it Smiley i totally got it wrong last night.
Was late... good to know you are always checking.

Re: why is the random() method not static?
Reply #6 - May 30th, 2009, 2:56pm
 
I don't know if this is a little off topic, but is it possible to do static import from the PDE For example, do:
import static java.lang.math.BigDecimal;
and you get:
expecting EOF
on the next line after the import.

I made an arbitrary precision square root example from the website  in the following code (also where I discovered static import) work out of the PDE, but I made my class extend BigDecimal so it could access its constants like ROUND_HALF_UP so I had to do super(0); which seems meaningless to me in this context.

Quote:
import java.math.*;

void setup() {
  println(new BigSqrt(new BigDecimal(5), 1000).getResult());
}
void draw() {
}

static class BigSqrt extends BigDecimal{
  /*
  the Babylonian square root method (Newton's method)
  http://www.lykkenborg.no/java/2005/03/computing-pi-using-bigdecimal.html
  */
  static final BigDecimal TWO = new BigDecimal(2);
  BigDecimal x0 = new BigDecimal("0");
  BigDecimal x1;
  BigSqrt(BigDecimal A, final int SCALE) {
    super(0);
    x1 = new BigDecimal(Math.sqrt(A.doubleValue()));
    while (!x0.equals(x1)) {
        x0 = x1;
        x1 = A.divide(x0, SCALE, ROUND_HALF_UP);
        x1 = x1.add(x0);
        x1 = x1.divide(TWO, SCALE, ROUND_HALF_UP);
    }
  }
  BigDecimal getResult() {
    return x1;
  }
}


I figure there is a better way to do this, correct
Re: why is the random() method not static?
Reply #7 - May 31st, 2009, 12:34am
 
Quote:
is it possible to do static import from the PDE?

Currently no. It is not a valid Java 1.4 syntax and currently we are stuck with this syntax (unless you put the code in a .java file).
Page Index Toggle Pages: 1