1

I am writing some code in processing to display a random walker using the Monte Carlo algorithm.

Now I have specified a walker class with inside two important methods a step function which controls the movement and a stepsize function which controls the speed and direction.

But for some reason I cannot properly call the stepsize method in the step method. As a result,the program draws nothing on the screen. The code executes, I have no errors.

My code:

import java.util.*;

class Walker {
    float y;
    float x;
    float monte_carlo;

    Walker() {
        x = width/2;
        y= height/2;
    }

    void display(){
        stroke(0);
        point(x,y);
    }

    float stepsize (float r1) {
        while (true) {
            r1 = random(0,10);
            float probability = r1;
            float r2 = random(0,10);
            if (r2 < probability) {
                return r1;
            }
        }
    }

    void step() {
        x += stepsize(monte_carlo);
        y += stepsize(monte_carlo);
    }   
}

Walker w;

void setup() {
    size(400,400);
    w = new Walker();
    background(255);
}

void draw() {  
    w.display();
    w.step();
}

This problem has been bothering me for a while and I would really appreciate it if someone could enlighten me!

Purple
  • 104
  • 13
Learner
  • 817
  • 3
  • 15
  • 23
  • what error do you get? – John B Sep 23 '13 at 14:44
  • Oh I'm sorry. I got no errors and the code executes, but it doesn't draw anything on the screen. – Learner Sep 23 '13 at 14:45
  • 1
    Are you receiving compilation errors? monte_carlo isn't initialized properly when you pass it to stepsize() and the variable r1 is useless since it just gets overwritten by the random method. – rfoo Sep 23 '13 at 14:46
  • Well, this program should draw something on the screen. The reason why it won't do that is that the monte carlo variable I declared in the class doesn't get modified and therefore the x and y variables in the step method won't either. I think I've said it wrong. The dot is being drawn, but it doesn't move. – Learner Sep 23 '13 at 14:49
  • rosscowar could you explain that? How is monte carlo not initialized properly? I'm not getting any compilation errors. I actually want it to be overwritten by the random method because I want random movement. Or am I not getting what you are saying? – Learner Sep 23 '13 at 14:53

2 Answers2

1

Java is pass-by-value, so your stepsize() method will not modify your monte_carlo variable. Just use the variable directly in the method instead of passing it in.

float stepsize () {
    while (true) {
        monte_carlo = random(0,10);
        float probability = monte_carlo;
        float r2 = random(0,10);
        if (r2 < probability) {
            return monte_carlo;
        }
    }
}
Community
  • 1
  • 1
GriffeyDog
  • 8,186
  • 3
  • 22
  • 34
0

You are always calling stepsize(0.0f) whenever stepsize(monte_carlo) is called, because float monte_carlo; means the variable is initialized with its default value (which is 0.0f) and you never modify it in your code.

Also, since you never use the value passed to the stepsize() function, you should use this signature instead :float stepsize().

aUserHimself
  • 1,589
  • 2
  • 17
  • 26