fork(2) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static int[] histogram(final int min, final int max, final int bucket, final int count) {
  11. // note, in this method, there's no need to know the actual values, just the range.
  12. final int range = max - min + 1;
  13. final int buckets = (range + bucket - 1) / bucket;
  14. final int[] results = new int[buckets];
  15. final Random rand = new Random();
  16. for (int i = 0; i < count; i++) {
  17. int value = rand.nextInt(range);
  18. results[value / bucket]++;
  19. }
  20. return results;
  21. }
  22.  
  23. public static void displayHistogram(final int first, final int bucketsize, final int maxbar, final int[] buckets) {
  24. int maxcount = getMax(buckets);
  25. int scale = ((maxcount + maxbar - 1) / maxbar) + 1;
  26. int width = (maxcount + scale - 1) / scale;
  27. String format = "%5d-%-5d | %-" + width + "s | %d\n";
  28. for (int i = 0; i < buckets.length; i++) {
  29. int start = (bucketsize * i) + first;
  30. int end = start + bucketsize - 1;
  31. System.out.printf(format, start, end, buildAsterisks(buckets[i], scale), buckets[i]);
  32. }
  33.  
  34.  
  35. System.out.printf("%11s | %s\n", "", lines(width));
  36. System.out.printf("%-11s | %s\n", "Scale", marks(maxcount, scale));
  37.  
  38. }
  39.  
  40. private static int getMax(int[] buckets) {
  41. int max = 0;
  42. for (int val : buckets) {
  43. max = Math.max(max, val);
  44. }
  45. return max;
  46. }
  47.  
  48. private static Object marks(int maxcount, int scale) {
  49. int width = (int)Math.log10(maxcount - 1) + 2;
  50. int count = ((maxcount + scale - 1) / scale) / width;
  51. StringBuilder sb = new StringBuilder();
  52. int mark = 0;
  53. int step = scale * width;
  54. for (int i = 0; i < count; i++) {
  55. mark += step;
  56. sb.append(String.format("%" + width + "d", mark));
  57. }
  58. return sb.toString();
  59. }
  60.  
  61. private static String lines(int width) {
  62. char[] array = new char[width];
  63. Arrays.fill(array, '-');
  64. return new String(array);
  65. }
  66.  
  67. private static String buildAsterisks(int size, int scale) {
  68. char[] array = new char[(size + scale - 1) / scale];
  69. Arrays.fill(array, '*');
  70. return new String(array);
  71. }
  72.  
  73. public static void main(String[] args) {
  74. int min = 1;
  75. int max = 100;
  76. int bsize = 10;
  77. int[] buckets = histogram(min, max, bsize, 1000);
  78. displayHistogram(min, bsize, 40, buckets);
  79. }
  80.  
  81. }
Success #stdin #stdout 0.08s 380160KB
stdin
Standard input is empty
stdout
    1-10    | *********************         | 84
   11-20    | **************************    | 102
   21-30    | ***************************** | 114
   31-40    | *************************     | 100
   41-50    | *************************     | 100
   51-60    | ************************      | 95
   61-70    | ************************      | 94
   71-80    | ***************************** | 113
   81-90    | ***********************       | 92
   91-100   | ***************************   | 106
            | -----------------------------
Scale       |   16  32  48  64  80  96 112