Guest User

Untitled

a guest
Jan 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. public class BrownianMotion {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. /**starts vars for program*/
  6.  
  7. int N = Integer.parseInt(args[0]);
  8. int T = Integer.parseInt(args[1]);
  9. double sqtotal = 0;
  10. double r;
  11. double avg;
  12.  
  13. /**number of trials loop*/
  14.  
  15. for (int count=0;count<T;count++) {
  16.  
  17. /**started here so that x & y reset at each trial*/
  18.  
  19. int x = 0;
  20. int y = 0;
  21.  
  22. /**loop for steps*/
  23. for (int steps=0;steps<N;steps++) {
  24.  
  25. r = Math.random();
  26. if (r < 0.25) x--;
  27. else if (r < 0.50) x++;
  28. else if (r < 0.75) y--;
  29. else if (r < 1.00) y++;
  30.  
  31. }
  32. /**squared total distance after each trial*/
  33. sqtotal = sqtotal + (x*x+y*y);
  34.  
  35. }
  36.  
  37. /**average of squared total*/
  38.  
  39. avg = sqtotal/T;
  40. System.out.println(avg);
  41.  
  42. }
  43. }
  44.  
  45. public class BrownianMotionThread extends Thread
  46. {
  47. int i;
  48. int T;
  49. int N;
  50. int numberOfProcessors;
  51. double sqtotal;
  52.  
  53. BrownianMotionThread(int i, int T, int N, int numberOfProcessors)
  54. {
  55. this.i = i;
  56. this.T = T;
  57. this.N = N;
  58. this.numberOfProcessors = numberOfProcessors;
  59.  
  60. }
  61.  
  62. public void run()
  63. {
  64. double r;
  65. for (int count=i;count<T;count+= numberOfProcessors) {
  66.  
  67. /**started here so that x & y reset at each trial*/
  68.  
  69. int x = 0;
  70. int y = 0;
  71. /**loop for steps*/
  72. for (int steps=0;steps<N;steps++) {
  73. r = Math.random();
  74. if (r < 0.25) x--;
  75. else if (r < 0.50) x++;
  76. else if (r < 0.75) y--;
  77. else if (r < 1.00) y++;
  78. }
  79. /**squared total distance after each trial*/
  80. sqtotal = sqtotal + (x*x+y*y);
  81. }
  82. }
  83. }
  84.  
  85. public class BrownianMotion {
  86. static double sqtotal;
  87. public static void main(String[] args) {
  88.  
  89. /**starts vars for program*/
  90.  
  91. final int N = Integer.parseInt(args[0]);
  92. final int T = Integer.parseInt(args[1]);
  93. final int numberOfProcessors = Runtime.getRuntime().availableProcessors();
  94. BrownianMotionThread[] threads = new BrownianMotionThread[numberOfProcessors];
  95. double avg;
  96.  
  97. /**number of trials loop*/
  98. for(int i = 0; i < numberOfProcessors; i++)
  99. {
  100. threads[i] = new BrownianMotionThread(i,T,N,numberOfProcessors);
  101. threads[i].start();
  102. }
  103. for(int i = 0; i < numberOfProcessors; i++)
  104. {
  105. try
  106. {
  107. threads[i].join();
  108. }
  109. catch (InterruptedException e)
  110. {
  111. e.printStackTrace();
  112. }
  113. }
  114.  
  115. for(int i = 0; i < numberOfProcessors; i++)
  116. {
  117. sqtotal += threads[i].sqtotal;
  118. }
  119.  
  120.  
  121. /**average of squared total*/
  122.  
  123. avg = sqtotal/T;
  124. System.out.println(avg);
  125.  
  126. }
  127.  
  128. }
Add Comment
Please, Sign In to add comment