Rnd.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.communityserver.util;
  16. import java.util.Random;
  17. public final class Rnd
  18. {
  19. private static final Random rnd = new Random();
  20. public static final double get() // get random number from 0 to 1
  21. {
  22. return rnd.nextDouble();
  23. }
  24. /**
  25. * Gets a random number from 0(inclusive) to n(exclusive)
  26. *
  27. * @param n The superior limit (exclusive)
  28. * @return A number from 0 to n-1
  29. */
  30. public static final int get(final int n) // get random number from 0 to n-1
  31. {
  32. return (int)(rnd.nextDouble() * n);
  33. }
  34. public static final int get(int min, int max) // get random number from min to max (not max-1 !)
  35. {
  36. return min + (int)Math.floor(rnd.nextDouble() * (max - min + 1));
  37. }
  38. public static final int nextInt(int n)
  39. {
  40. return (int)Math.floor(rnd.nextDouble() * n);
  41. }
  42. public static final int nextInt()
  43. {
  44. return rnd.nextInt();
  45. }
  46. public static final double nextDouble()
  47. {
  48. return rnd.nextDouble();
  49. }
  50. public static final double nextGaussian()
  51. {
  52. return rnd.nextGaussian();
  53. }
  54. public static final boolean nextBoolean()
  55. {
  56. return rnd.nextBoolean();
  57. }
  58. public static final void nextBytes(byte[] array)
  59. {
  60. rnd.nextBytes(array);
  61. }
  62. }