Advertisement
hocikto19

Drunken Sailors

Feb 26th, 2015
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. import java.util.*;
  2. public class Sailors{
  3.   static int N=10000; //number of sailors
  4.   static double [] sls = new double[N];//actual sailor coordinates
  5.   static double step = 1.;//step size
  6.   static Random rnd = new Random();
  7.   static int currenttime=0;
  8.  
  9.   static void makestep(){
  10.     for(int i=0;i<N;i++){
  11.     if(rnd.nextDouble()<0.5)
  12.             sls[i]+=step;
  13.         else
  14.             sls[i]-=step;
  15.     }
  16.   }
  17.   static void statistics(){
  18.     double sum=0; //sum of coordinates
  19.     double sum2=0;//sum of coordinate squares
  20.     for(int i=0;i<N;i++){
  21.     sum+=sls[i];
  22.         sum2+=sls[i]*sls[i];
  23.     }
  24.     double mean = sum/N;
  25.     double meansquare = sum2/N;
  26.     System.out.println("Time   "+currenttime+"  Mean    "+mean+"   meansquare   "+meansquare+"  rooted meansquare   "+Math.sqrt(meansquare));
  27.   }
  28.   public static void main(String[] args){
  29.     for(int i=1;i<100;i++){
  30.       currenttime=i;
  31.       makestep();
  32.       statistics();
  33.     }
  34.   }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement