Rnd.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.util;
  16. /**$
  17. *
  18. * @author Balancer
  19. *
  20. */
  21. public class Rnd
  22. {
  23. public static final double get() // get random number from 0 to 1
  24. {
  25. return MTRandom.getInstance().getSecureRandom().nextDouble();
  26. }
  27. /**
  28. * Gets a random number from 0(inclusive) to n(exclusive)
  29. *
  30. * @param n The superior limit (exclusive)
  31. * @return A number from 0 to n-1
  32. */
  33. public static final int get(int n) // get random number from 0 to n-1
  34. {
  35. return (int) (MTRandom.getInstance().getSecureRandom().nextDouble() * n);
  36. }
  37. public static final int get(int min, int max) // get random number from min to max (not max-1 !)
  38. {
  39. return min + (int) Math.floor(MTRandom.getInstance().getSecureRandom().nextDouble() * (max - min + 1));
  40. }
  41. public static final int nextInt(int n)
  42. {
  43. return (int) Math.floor(MTRandom.getInstance().getSecureRandom().nextDouble() * n);
  44. }
  45. public static final int nextInt()
  46. {
  47. return MTRandom.getInstance().getSecureRandom().nextInt();
  48. }
  49. public static final double nextDouble()
  50. {
  51. return MTRandom.getInstance().getSecureRandom().nextDouble();
  52. }
  53. public static final double nextGaussian()
  54. {
  55. return MTRandom.getInstance().getSecureRandom().nextGaussian();
  56. }
  57. public static final boolean nextBoolean()
  58. {
  59. return MTRandom.getInstance().getSecureRandom().nextBoolean();
  60. }
  61. public static final void nextBytes(final byte[] array)
  62. {
  63. MTRandom.getInstance().getSecureRandom().nextBytes(array);
  64. }
  65. }