fork download
  1. // example code for: http://stackoverflow.com/a/21976027/616460
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7.  
  8. // the important bits are the implementations of next() below. everything
  9. // else is (somewhat sloppy) code to support testing and output.
  10. class Dist {
  11.  
  12. // modify MAX_VALUE and RUN_COUNT as you see fit
  13. static final int MAX_VALUE = 60;
  14. static final int RUN_COUNT = 10000;
  15.  
  16. static final Random random = new Random();
  17.  
  18. // base class for a test; generates a set of RUN_COUNT random values
  19. // in the range [0, MAX_VALUE) and stores a histogram of the results.
  20. // subclasses should implement next() to return a value [0, MAXVALUE).
  21. static abstract class RandomTest {
  22. final int[] count = new int[MAX_VALUE];
  23. final String name;
  24. RandomTest (String name) { this.name = name; }
  25. RandomTest run () {
  26. System.out.println("Running " + name + "...");
  27. for (int n = 0; n < RUN_COUNT; ++ n) count[next()] ++;
  28. return this;
  29. }
  30. abstract int next ();
  31. }
  32.  
  33. // Uniform distribution
  34. static class Uniform extends RandomTest {
  35. Uniform () { super("Uniform"); }
  36. @Override int next () {
  37. return random.nextInt(MAX_VALUE);
  38. }
  39. }
  40.  
  41. // pow(value, bias)
  42. static class Pow extends RandomTest {
  43. final double bias;
  44. Pow (double bias) { super("Pow(" + bias + ")"); this.bias = bias; }
  45. @Override int next () {
  46. double v = Math.pow(random.nextDouble(), bias);
  47. return (int)(v * MAX_VALUE);
  48. }
  49. }
  50.  
  51. // [0,1) section of gaussian distribution, scaled by given value
  52. static class Gaussian extends RandomTest {
  53. final double scale;
  54. Gaussian (double scale) { super("G(" + scale + ")"); this.scale = scale; }
  55. @Override int next () {
  56. double v;
  57. do {
  58. v = Math.abs(random.nextGaussian() / scale);
  59. } while (v >= 1.0);
  60. return (int)(v * MAX_VALUE);
  61. }
  62. }
  63.  
  64. // cubic S
  65. static class Cubic extends RandomTest {
  66. Cubic () { super("Cubic"); }
  67. @Override int next () {
  68. double v = random.nextDouble();
  69. v = 3*v*v - 2*v*v*v;
  70. return (int)(v * MAX_VALUE);
  71. }
  72. }
  73.  
  74. // print one row of results; prints header if index == -1
  75. static void printResultRow (List<RandomTest> tests, int index) {
  76. System.out.printf("%-5s:", (index < 0) ? "Name" : Integer.toString(index));
  77. for (RandomTest test:tests) {
  78. String s = (index < 0) ? test.name : Integer.toString(test.count[index]);
  79. System.out.printf("%9s ", s);
  80. }
  81. System.out.println();
  82. }
  83.  
  84. // run all tests and print results
  85. public static void main (String[] args) {
  86.  
  87. List<RandomTest> tests = new ArrayList<RandomTest>();
  88. tests.add(new Uniform().run());
  89. tests.add(new Pow(0.5).run());
  90. tests.add(new Pow(2.0).run());
  91. tests.add(new Pow(10.0).run());
  92. tests.add(new Gaussian(1.0).run());
  93. tests.add(new Gaussian(3.5).run());
  94. tests.add(new Cubic().run());
  95.  
  96. for (int n = -1; n < MAX_VALUE; ++ n)
  97. printResultRow(tests, n);
  98.  
  99. }
  100.  
  101. }
  102.  
Success #stdin #stdout 0.35s 59388KB
stdin
Standard input is empty
stdout
Running Uniform...
Running Pow(0.5)...
Running Pow(2.0)...
Running Pow(10.0)...
Running G(1.0)...
Running G(3.5)...
Running Cubic...
Name :  Uniform  Pow(0.5)  Pow(2.0) Pow(10.0)    G(1.0)    G(3.5)     Cubic 
0    :      172         1      1254      6575       190       496       781 
1    :      153         7       565       463       173       477       331 
2    :      148        13       406       304       187       449       280 
3    :      162        23       319       194       213       466       221 
4    :      167        30       286       170       204       456       199 
5    :      167        30       272       147       202       406       162 
6    :      199        32       262       119       216       435       181 
7    :      163        60       243       100       201       426       156 
8    :      157        35       208       108       194       444       142 
9    :      181        62       232        92       190       408       141 
10   :      167        69       192        88       172       377       154 
11   :      163        63       204        83       194       384       130 
12   :      157        62       188        74       209       353       139 
13   :      189        61       173        70       185       350       101 
14   :      169        85       164        67       182       335       114 
15   :      175        97       171        62       180       309       106 
16   :      206        93       160        49       205       288       144 
17   :      150        90       142        57       206       294       137 
18   :      154       106       160        33       173       256       126 
19   :      155       110       141        45       178       249       118 
20   :      154        93       147        57       157       237       118 
21   :      175       129       148        54       169       194       102 
22   :      166       126       144        43       196       212       134 
23   :      164       128       129        41       165       177       114 
24   :      148       140       132        40       170       153       112 
25   :      190       153       113        43       168       159        94 
26   :      178       146       134        29       169       134       102 
27   :      166       161       111        33       166       122       111 
28   :      205       154       114        37       175       102       120 
29   :      162       170       123        27       153        94       101 
30   :      174       166       114        28       169        92        98 
31   :      161       174       112        35       172        66        99 
32   :      166       204       102        41       169        58       120 
33   :      158       181       135        30       149        68       113 
34   :      169       183        99        34       164        58       110 
35   :      167       189        94        24       178        50       118 
36   :      160       203       118        32       152        43       108 
37   :      158       218        94        33       165        41       104 
38   :      184       192       100        24       189        43       116 
39   :      152       217        94        22       165        35       128 
40   :      143       239        97        24       180        35       123 
41   :      171       224        91        19       174        35       123 
42   :      143       255        95        22       172        21       119 
43   :      178       260       113        18       153        15       114 
44   :      158       241       102        30       150        21       128 
45   :      176       261        96        20       171        12       131 
46   :      185       246       104        14       145        20       131 
47   :      157       295       108        23       134        10       139 
48   :      174       260       100        13       146         6       144 
49   :      168       274        86        27       128         1       133 
50   :      175       274        98        16       132         6       144 
51   :      146       280        96        24       138         3       166 
52   :      162       277       106        24       114         4       157 
53   :      161       299        88        15       117         2       197 
54   :      173       276        88        20       126         3       180 
55   :      151       272        79        16       107         6       203 
56   :      164       318        93        15       120         2       225 
57   :      162       317        91        21       124         0       254 
58   :      183       343        86        17       127         2       338 
59   :      159       333        84        15       128         0       766