Friday, January 30, 2015

RandomSurfer.java pgm



RandomSurfer.java

public class RandomSurfer 
{
 public static void main(String[] args) 
{
        int T = Integer.parseInt(args[0]);    
        int M = StdIn.readInt();             
        int N = StdIn.readInt();              
        double[][] p = new double[N][N];     
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++) 
                p[i][j] = StdIn.readDouble(); 
        int[] freq = new int[N];            
         int page = 0;
         for (int t = 0; t < T; t++) 
            {
            double r = Math.random(); 
            double sum = 0.0; 
            for (int j = 0; j < N; j++) 
           {
                         sum += p[page][j]; 
            if (r < sum) { page = j; break;
            } 
            } 
            freq[page]++; 
        } 
 
       for (int i = 0; i < N; i++) 
      {
            StdOut.printf("%8.5f", (double) freq[i] / T); 
        }
        StdOut.println(); 
    }  
} 

No comments:

Post a Comment