vendredi 11 septembre 2015

Java: pass the same randomly generated number onto another function

my question is the following: How do I use the same random integer twice without it generating a new random number? this is my code:

private static void draw() {
    for (int a=random(2,4); a>0; a--) {
        rectangle(random(0,20), random(0,10));
    }       
}

private static void rectangle(int width, int height) {
    halfRect(width, height);
    halfRect(width, height);

}

private static void halfRect(int width, int height) {
    walkRandom(width, true);
    turnLeft();
    walkRandom(height, true);
}

I want to use the in line 3 randomly generated integers in the function rectangle, which calls the function halfRect twice. I want the random integers to stay the same the two times it runs the function, which is not the case with this code. Also I'd rather not use variables, it's a school assignment and they they it could be done without variables. Any suggestions? thanks!




Aucun commentaire:

Enregistrer un commentaire