Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Randomwalk - average value help

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Randomwalk - average value help

    Hi, I've set up my Randomwalk program and it's working wonders. Before going to my question, I'd like to quickly introduce what my randomwalk program does:
    It has two main parts in the code, the Steps part and the Walks part. It has 500 steps per walk, and 1000 walks in total. In each walk it can either go left or right (-1 or +1) and after 500 steps, it'll end up in a random location (most likely between -30 and + 30 and most likely near 0). This "end location" L will be outputted 1000 times, and then the program will output how many times it landed in -500 to +500 ( like it landed it 4 ten times).

    My teacher just told me that I need to implement a new feature. That feature is finding the average value. This is found by:

    I need to say (let's call it a) a = ((Where it landed * how many times it landed on that spot)+(Where it landed+1 * how many times it landed on that spot+1)+..)/number of walks, in this case 1000)

    Example: a = ((-500*0)+(-499*0)...(10*3)...(500*0))/(1000)

    In the example, you can see that it starts off saying that a equals how many times it landed on -500*-500 and so on until it reaches how many times it landed on +500*500, then divided by the number of walks, which is 1000.
    I'm not sure how to implement this feature, so I'd be grateful if you could help me Heres the full code:

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    import java.util.HashMap;
     
    public class RandomWalk {
     
        public static final int NBR_WALKS = 1000;
        public static final int NBR_STEPS = 500;
     
        public static void main(String[] args) {
        	/*PrintStream out;
            try {
            	out = new PrintStream(new FileOutputStream("/Volumes/DATA/Desktop/RandomWalkResultater.txt", true));
            	System.setOut(out);
            } catch (FileNotFoundException e) {
            	e.printStackTrace();
            }*/
        	HashMap<Integer, Integer> totals = new HashMap<>();
            for (int i = -NBR_STEPS; i <= NBR_STEPS; i++) {
                totals.put(i, 0);
            }
            System.out.println("");
            for (int i = 0; i < NBR_WALKS; i++) {
                int total_value = 0;
                for (int j = 0; j < NBR_STEPS; j++) {
                    int L = (int) (Math.random() * 2);
                    total_value += (L == 0) ? -1 : 1;
                }
     
                System.out.println("For randomwalk number " + i + " the end point was " + total_value);
                totals.put(total_value, totals.get(total_value) + 1);
     
            }
            System.out.println(" ");
            for (int i = -500; i < 500; i++) {
     
            System.out.println("You landed on the end point "+i+" the following times: "+totals.get(i));
            }
            System.out.println(" ");
            System.out.println("For the excel program:");
            System.out.println("x-values:");
            for (int i = -500; i < 500; i++) {
            System.out.println(i);
            }
            System.out.println("");
            System.out.println("y-values:");
            for (int i = -500; i < 500; i++) {
            System.out.println(totals.get(i));
     
            }
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Randomwalk - average value help

    This sounds like a frequency counter, so forgive me if I'm misunderstanding.

    A simple frequency counter is an array of as many elements as there are possible values chosen (or 'step' values?). In this case, I'm not sure if it's 500 or 1,000, but no matter. So for each 'step' the corresponding array element is incremented. For example, if the step lands on number '125', then freqArray[125]++. When done, each element of the array will contain the number of times that value was chosen. You can then do with the resulting frequency counts as needed to obtain the desired statistics for each walk. If there is no '0' value, then you may have to adjust the array elements by one to translate the frequency array to your value field.

Similar Threads

  1. Calculate the average
    By Anais in forum Java Theory & Questions
    Replies: 5
    Last Post: November 16th, 2013, 07:57 AM
  2. Help with Average and Variables Please =)
    By Raymond Pittman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 9th, 2011, 10:02 AM
  3. Moving Average
    By Donieob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 3rd, 2011, 08:58 AM
  4. Finding the Average
    By KiwiFlan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 16th, 2010, 07:58 PM
  5. Moving average
    By bondage in forum Loops & Control Statements
    Replies: 0
    Last Post: May 7th, 2010, 04:20 AM