Random.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package holeg.utility.math;
  2. /**
  3. * A Helper class to abstract the implementation of the generation of a random number.
  4. */
  5. public class Random {
  6. private static final java.util.Random random = new java.util.Random();
  7. /**
  8. * True or false
  9. *
  10. * @return the random boolean.
  11. */
  12. public static boolean nextBoolean() {
  13. return random.nextBoolean();
  14. }
  15. /**
  16. * Between 0.0(inclusive) and 1.0 (exclusive)
  17. *
  18. * @return the random double.
  19. */
  20. public static double nextDouble() {
  21. return random.nextDouble();
  22. }
  23. public static float nextFloatInRange(float min, float max) {
  24. return min + random.nextFloat() * Math.abs(max - min);
  25. }
  26. /**
  27. * Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean and
  28. * standard deviation from this random number generator's sequence.
  29. *
  30. * @param mean the mean of the Gaussian distribution
  31. * @param deviation the standard deviation of the Gaussian distribution
  32. * @return next gaussian value
  33. */
  34. public static double nextGaussian(double mean, double deviation) {
  35. return mean + random.nextGaussian() * deviation;
  36. }
  37. /**
  38. * Random Double in Range [min;max[ with UniformDistribution
  39. *
  40. * @param min minimum double value
  41. * @param max maximum double value
  42. * @return next random double value in range
  43. */
  44. public static double nextDoubleInRange(double min, double max) {
  45. return min + random.nextDouble() * Math.abs(max - min);
  46. }
  47. /**
  48. * Random Int in Range [min;max[ with UniformDistribution
  49. *
  50. * @param min minimum integer value
  51. * @param max maximum integer value
  52. * @return next random integer value in range
  53. */
  54. public static int nextIntegerInRange(int min, int max) {
  55. return min + random.nextInt(max - min);
  56. }
  57. }