Advertisement
richarduie

CountRandoms.java

Oct 9th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.util.Random;
  6.  
  7. // generate random integers and report their frequencies of occurrence
  8. public class CountRandoms
  9. {
  10.     public static void main (String[] args) {
  11.         // random integers will be generated in the range [1, biggest]
  12.         int biggest = 10;
  13.         // how many random values to generate
  14.         int howMany = 1000;
  15.         // filename to which to write report
  16.         String rptFile = "countRandomsRpt.txt";
  17.  
  18.         // generate the vales and write the report
  19.         saveReport(
  20.             getCountsReport(
  21.                 getFrequency(
  22.                     biggest,
  23.                     getRandoms( biggest, howMany )
  24.                 )
  25.             ),
  26.             rptFile
  27.         );
  28.     }
  29.    
  30.     // generate howMany random integers in the range [1, biggest]
  31.     public static int[] getRandoms( int biggest, int howMany ) {
  32.         // allocate array to hold random integers
  33.         int[] r = new int[ howMany ];
  34.         // create new Random object
  35.         Random rand = new Random();
  36.         // load new, random integer in range [1, biggest] to array
  37.         for (int i = 0; i < howMany; i++) {
  38.             r[ i ] = 1 + rand.nextInt( biggest );
  39.         }
  40.         return r;
  41.     }
  42.     // count the frequency of occurrence of each integer in the
  43.     // range [1, biggest] in the array r
  44.     public static int[] getFrequency( int biggest, int[] r ) {
  45.         // allocate array to hold frequency count for each integer
  46.         // in the range [1, biggest]
  47.         int[] freq = new int[ biggest ];
  48.         for (int i = 0; i < r.length; i++) {
  49.             freq[ r[ i ] - 1 ]++;
  50.         }
  51.         return freq;
  52.     }
  53.     // compose a report of freq(uency) counts
  54.     public static String getCountsReport( int[] freq ) {
  55.         // total up the frequencies and identify the number of digits
  56.         // in the largest frequency
  57.         int maxFWidth =  0;
  58.         int howMany = 0;
  59.         for (int i = 0; i < freq.length; i++) {
  60.             howMany += freq[ i ];
  61.             maxFWidth = Math.max(maxFWidth, freq[ i ]);
  62.         }
  63.         maxFWidth = ("" + maxFWidth).length();
  64.         // number of digits in biggest value
  65.         int maxNWidth = ("" + freq.length).length();
  66.         // collect report output
  67.         StringBuffer rpt = new StringBuffer();
  68.         // add report headings
  69.         rpt.append(
  70.             "Random Number Frequency Counts Report\n" +
  71.             "-------------------------------------\n" +
  72.             "For " + howMany +" random integer in the range [1, " +
  73.             freq.length + "]:\n\n"
  74.         );
  75.         // add frequency detail for each number
  76.         for (int i = 0; i < freq.length; i++) {
  77.             rpt.append(
  78.                 "   " + padLeft( maxNWidth, i + 1) + " occurred " +
  79.                 padLeft( maxFWidth, freq[ i ] ) + " times\n"
  80.             );
  81.         }
  82.         return rpt.toString();
  83.     }
  84.     // write report string rpt to file named rptFile
  85.     public static void saveReport( String rpt, String rptFile ) {
  86.         // allocate report file and stream for writing
  87.         File outfile = new File( rptFile );
  88.         FileOutputStream out = null;
  89.         try { out = new FileOutputStream( outfile ); }
  90.         catch ( FileNotFoundException e ) {
  91.             // probably should do something more useful
  92.             System.out.println( "Error!!! File Not Found\n" + e.toString() );
  93.         }
  94.         try { out.write( rpt.getBytes() ); }
  95.         catch ( IOException e ){
  96.             // probably should do something more useful
  97.             System.out.println(
  98.                 "reportCounts() failed trying to write a record to " +
  99.                 rptFile + "\n" + e.toString()
  100.             );
  101.         }
  102.         try { out.close();   }
  103.         catch (IOException e) {
  104.             // probably should do something more useful
  105.             System.out.println(
  106.                 "reportCounts() failed trying to close " +
  107.                 rptFile + "\n" + e.toString()
  108.             );
  109.         }
  110.     }
  111.     // convert an int to string of given width, padded with blanks on
  112.     // the left-hand side
  113.     public static String padLeft( int width, int n) {
  114.         String p = "" + n;
  115.         while ( width > p.length() ) {
  116.             p = " " + p;
  117.         }
  118.         return p;
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement