Random.java 1.9 KB

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