Random.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package holeg.utility.math;
  2. public class Random {
  3. private static java.util.Random random = new java.util.Random();
  4. /**
  5. * True or false
  6. *
  7. * @return the random boolean.
  8. */
  9. public static boolean nextBoolean() {
  10. return random.nextBoolean();
  11. }
  12. /**
  13. * Between 0.0(inclusive) and 1.0 (exclusive)
  14. *
  15. * @return the random double.
  16. */
  17. public static double nextDouble() {
  18. return random.nextDouble();
  19. }
  20. public static float nextFloatInRange(float min, float max) {
  21. return min + random.nextFloat() * Math.abs(max - min);
  22. }
  23. /**
  24. * Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean and
  25. * standard deviation from this random number generator's sequence.
  26. *
  27. * @param mean
  28. * @param deviation
  29. * @return
  30. */
  31. public static double nextGaussian(double mean, double deviation) {
  32. return mean + random.nextGaussian() * deviation;
  33. }
  34. public static double nextDoubleInRange(double min, double max) {
  35. return min + random.nextDouble() * Math.abs(max - min);
  36. }
  37. /**
  38. * Random Int in Range [min;max[ with UniformDistirbution
  39. *
  40. * @param min
  41. * @param max
  42. * @return
  43. */
  44. public static int nextIntegerInRange(int min, int max) {
  45. return min + random.nextInt(max - min);
  46. }
  47. /**
  48. * Random Int in Range [min;max[ with UniformDistirbution
  49. *
  50. * @param min
  51. * @param max
  52. * @param valueBetween a value between min and max
  53. * @return
  54. */
  55. public static int nextIntegerInRangeExcept(int min, int max, int valueBetween) {
  56. int result = min;
  57. if (max - min == 1) {
  58. //return the other value
  59. return (valueBetween == min) ? max : min;
  60. }
  61. try {
  62. result = min + random.nextInt((max - 1) - min);
  63. if (result >= valueBetween) {
  64. result++;
  65. }
  66. } catch (java.lang.IllegalArgumentException e) {
  67. System.err.println("min : " + min + " max : " + max + " valueBetween:" + valueBetween);
  68. System.err.println("Except max should be more then min");
  69. }
  70. return result;
  71. }
  72. }