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 = 30;
  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(20.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.24s 61304KB
stdin
Standard input is empty
stdout
Running Uniform...
Running Pow(0.5)...
Running Pow(2.0)...
Running Pow(20.0)...
Running G(1.0)...
Running G(3.5)...
Running Cubic...
Name :  Uniform  Pow(0.5)  Pow(2.0) Pow(20.0)    G(1.0)    G(3.5)     Cubic 
0    :      351        11      1790      8464       387       858      1104 
1    :      325        30       781       288       398       912       455 
2    :      328        55       578       181       391       862       367 
3    :      319        86       493       118       372       880       310 
4    :      350       107       442        91       378       787       305 
5    :      350       126       420        85       379       824       270 
6    :      317       147       363        67       380       662       264 
7    :      321       160       319        58       423       643       247 
8    :      318       181       336        51       382       553       242 
9    :      341       222       295        50       370       515       235 
10   :      339       222       304        44       376       474       246 
11   :      326       272       257        40       348       395       225 
12   :      357       287       242        52       375       319       258 
13   :      333       292       250        36       343       300       233 
14   :      351       333       249        30       347       220       219 
15   :      317       374       242        26       329       185       231 
16   :      350       355       225        29       319       139       212 
17   :      358       387       220        41       307       115       253 
18   :      343       431       231        15       322       100       229 
19   :      330       459       200        25       327        70       233 
20   :      332       412       208        37       298        56       224 
21   :      315       436       190        19       296        37       243 
22   :      312       505       178        25       289        30       252 
23   :      365       533       176        17       283        20       246 
24   :      323       503       182        19       261        18       285 
25   :      331       546       180        11       280        13       308 
26   :      334       615       176        28       280         5       352 
27   :      304       580       161        15       274         4       370 
28   :      321       658       169        14       251         1       452 
29   :      339       675       143        24       235         3      1130