Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BrownianMotion {
- public static void main(String[] args) {
- /**starts vars for program*/
- int N = Integer.parseInt(args[0]);
- int T = Integer.parseInt(args[1]);
- double sqtotal = 0;
- double r;
- double avg;
- /**number of trials loop*/
- for (int count=0;count<T;count++) {
- /**started here so that x & y reset at each trial*/
- int x = 0;
- int y = 0;
- /**loop for steps*/
- for (int steps=0;steps<N;steps++) {
- r = Math.random();
- if (r < 0.25) x--;
- else if (r < 0.50) x++;
- else if (r < 0.75) y--;
- else if (r < 1.00) y++;
- }
- /**squared total distance after each trial*/
- sqtotal = sqtotal + (x*x+y*y);
- }
- /**average of squared total*/
- avg = sqtotal/T;
- System.out.println(avg);
- }
- }
- public class BrownianMotionThread extends Thread
- {
- int i;
- int T;
- int N;
- int numberOfProcessors;
- double sqtotal;
- BrownianMotionThread(int i, int T, int N, int numberOfProcessors)
- {
- this.i = i;
- this.T = T;
- this.N = N;
- this.numberOfProcessors = numberOfProcessors;
- }
- public void run()
- {
- double r;
- for (int count=i;count<T;count+= numberOfProcessors) {
- /**started here so that x & y reset at each trial*/
- int x = 0;
- int y = 0;
- /**loop for steps*/
- for (int steps=0;steps<N;steps++) {
- r = Math.random();
- if (r < 0.25) x--;
- else if (r < 0.50) x++;
- else if (r < 0.75) y--;
- else if (r < 1.00) y++;
- }
- /**squared total distance after each trial*/
- sqtotal = sqtotal + (x*x+y*y);
- }
- }
- }
- public class BrownianMotion {
- static double sqtotal;
- public static void main(String[] args) {
- /**starts vars for program*/
- final int N = Integer.parseInt(args[0]);
- final int T = Integer.parseInt(args[1]);
- final int numberOfProcessors = Runtime.getRuntime().availableProcessors();
- BrownianMotionThread[] threads = new BrownianMotionThread[numberOfProcessors];
- double avg;
- /**number of trials loop*/
- for(int i = 0; i < numberOfProcessors; i++)
- {
- threads[i] = new BrownianMotionThread(i,T,N,numberOfProcessors);
- threads[i].start();
- }
- for(int i = 0; i < numberOfProcessors; i++)
- {
- try
- {
- threads[i].join();
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- for(int i = 0; i < numberOfProcessors; i++)
- {
- sqtotal += threads[i].sqtotal;
- }
- /**average of squared total*/
- avg = sqtotal/T;
- System.out.println(avg);
- }
- }
Add Comment
Please, Sign In to add comment