Rnd.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.util;
  20. import java.security.SecureRandom;
  21. import java.util.Random;
  22. /**
  23. * @author Forsaiken
  24. */
  25. public final class Rnd
  26. {
  27. /**
  28. * This class extends {@link java.util.Random} but do not compare and store atomically.<br>
  29. * Instead it`s using a simple volatile flag to ensure reading and storing the whole 64bit seed chunk.<br>
  30. * This implementation is much faster on parallel access, but may generate the same seed for 2 threads.
  31. * @author Forsaiken
  32. * @see java.util.Random
  33. */
  34. public static final class NonAtomicRandom extends Random
  35. {
  36. private static final long serialVersionUID = 1L;
  37. private volatile long _seed;
  38. public NonAtomicRandom()
  39. {
  40. this(++SEED_UNIQUIFIER + System.nanoTime());
  41. }
  42. public NonAtomicRandom(final long seed)
  43. {
  44. setSeed(seed);
  45. }
  46. @Override
  47. public final int next(final int bits)
  48. {
  49. return (int) ((_seed = ((_seed * MULTIPLIER) + ADDEND) & MASK) >>> (48 - bits));
  50. }
  51. @Override
  52. public final void setSeed(final long seed)
  53. {
  54. _seed = (seed ^ MULTIPLIER) & MASK;
  55. }
  56. }
  57. /**
  58. * @author Forsaiken
  59. */
  60. protected static final class RandomContainer
  61. {
  62. private final Random _random;
  63. protected RandomContainer(final Random random)
  64. {
  65. _random = random;
  66. }
  67. public final Random directRandom()
  68. {
  69. return _random;
  70. }
  71. /**
  72. * Get a random double number from 0 to 1
  73. * @return A random double number from 0 to 1
  74. * @see com.l2jserver.util.Rnd#nextDouble()
  75. */
  76. public final double get()
  77. {
  78. return _random.nextDouble();
  79. }
  80. /**
  81. * Gets a random integer number from 0(inclusive) to n(exclusive)
  82. * @param n The superior limit (exclusive)
  83. * @return A random integer number from 0 to n-1
  84. */
  85. public final int get(final int n)
  86. {
  87. return (int) (_random.nextDouble() * n);
  88. }
  89. /**
  90. * Gets a random integer number from min(inclusive) to max(inclusive)
  91. * @param min The minimum value
  92. * @param max The maximum value
  93. * @return A random integer number from min to max
  94. */
  95. public final int get(final int min, final int max)
  96. {
  97. return min + (int) (_random.nextDouble() * ((max - min) + 1));
  98. }
  99. /**
  100. * Gets a random long number from min(inclusive) to max(inclusive)
  101. * @param min The minimum value
  102. * @param max The maximum value
  103. * @return A random long number from min to max
  104. */
  105. public final long get(final long min, final long max)
  106. {
  107. return min + (long) (_random.nextDouble() * ((max - min) + 1));
  108. }
  109. /**
  110. * Get a random boolean state (true or false)
  111. * @return A random boolean state (true or false)
  112. * @see java.util.Random#nextBoolean()
  113. */
  114. public final boolean nextBoolean()
  115. {
  116. return _random.nextBoolean();
  117. }
  118. /**
  119. * Fill the given array with random byte numbers from Byte.MIN_VALUE(inclusive) to Byte.MAX_VALUE(inclusive)
  120. * @param array The array to be filled with random byte numbers
  121. * @see java.util.Random#nextBytes(byte[] bytes)
  122. */
  123. public final void nextBytes(final byte[] array)
  124. {
  125. _random.nextBytes(array);
  126. }
  127. /**
  128. * Get a random double number from 0 to 1
  129. * @return A random double number from 0 to 1
  130. * @see java.util.Random#nextDouble()
  131. */
  132. public final double nextDouble()
  133. {
  134. return _random.nextDouble();
  135. }
  136. /**
  137. * Get a random float number from 0 to 1
  138. * @return A random integer number from 0 to 1
  139. * @see java.util.Random#nextFloat()
  140. */
  141. public final float nextFloat()
  142. {
  143. return _random.nextFloat();
  144. }
  145. /**
  146. * Get a random gaussian double number from 0 to 1
  147. * @return A random gaussian double number from 0 to 1
  148. * @see java.util.Random#nextGaussian()
  149. */
  150. public final double nextGaussian()
  151. {
  152. return _random.nextGaussian();
  153. }
  154. /**
  155. * Get a random integer number from Integer.MIN_VALUE(inclusive) to Integer.MAX_VALUE(inclusive)
  156. * @return A random integer number from Integer.MIN_VALUE to Integer.MAX_VALUE
  157. * @see java.util.Random#nextInt()
  158. */
  159. public final int nextInt()
  160. {
  161. return _random.nextInt();
  162. }
  163. /**
  164. * Get a random long number from Long.MIN_VALUE(inclusive) to Long.MAX_VALUE(inclusive)
  165. * @return A random integer number from Long.MIN_VALUE to Long.MAX_VALUE
  166. * @see java.util.Random#nextLong()
  167. */
  168. public final long nextLong()
  169. {
  170. return _random.nextLong();
  171. }
  172. }
  173. /**
  174. * @author Forsaiken
  175. */
  176. public static enum RandomType
  177. {
  178. /**
  179. * For best random quality.
  180. * @see java.security.SecureRandom
  181. */
  182. SECURE,
  183. /**
  184. * For average random quality.
  185. * @see java.util.Random
  186. */
  187. UNSECURE_ATOMIC,
  188. /**
  189. * Like {@link com.l2jserver.util.Rnd.RandomType#UNSECURE_ATOMIC}.<br>
  190. * Each thread has it`s own random instance.<br>
  191. * Provides best parallel access speed.
  192. * @see com.l2jserver.util.Rnd.ThreadLocalRandom
  193. */
  194. UNSECURE_THREAD_LOCAL,
  195. /**
  196. * Like {@link com.l2jserver.util.Rnd.RandomType#UNSECURE_ATOMIC}.<br>
  197. * Provides much faster parallel access speed.
  198. * @see com.l2jserver.util.Rnd.NonAtomicRandom
  199. */
  200. UNSECURE_VOLATILE
  201. }
  202. /**
  203. * This class extends {@link java.util.Random} but do not compare and store atomically.<br>
  204. * Instead it`s using thread local ensure reading and storing the whole 64bit seed chunk.<br>
  205. * This implementation is the fastest, never generates the same seed for 2 threads.<br>
  206. * Each thread has it`s own random instance.
  207. * @author Forsaiken
  208. * @see java.util.Random
  209. */
  210. public static final class ThreadLocalRandom extends Random
  211. {
  212. private static final class Seed
  213. {
  214. long _seed;
  215. Seed(final long seed)
  216. {
  217. setSeed(seed);
  218. }
  219. final int next(final int bits)
  220. {
  221. return (int) ((_seed = ((_seed * MULTIPLIER) + ADDEND) & MASK) >>> (48 - bits));
  222. }
  223. final void setSeed(final long seed)
  224. {
  225. _seed = (seed ^ MULTIPLIER) & MASK;
  226. }
  227. }
  228. private static final long serialVersionUID = 1L;
  229. private final ThreadLocal<Seed> _seedLocal;
  230. public ThreadLocalRandom()
  231. {
  232. _seedLocal = new ThreadLocal<Seed>()
  233. {
  234. @Override
  235. public final Seed initialValue()
  236. {
  237. return new Seed(++SEED_UNIQUIFIER + System.nanoTime());
  238. }
  239. };
  240. }
  241. public ThreadLocalRandom(final long seed)
  242. {
  243. _seedLocal = new ThreadLocal<Seed>()
  244. {
  245. @Override
  246. public final Seed initialValue()
  247. {
  248. return new Seed(seed);
  249. }
  250. };
  251. }
  252. @Override
  253. public final int next(final int bits)
  254. {
  255. return _seedLocal.get().next(bits);
  256. }
  257. @Override
  258. public final void setSeed(final long seed)
  259. {
  260. if (_seedLocal != null)
  261. {
  262. _seedLocal.get().setSeed(seed);
  263. }
  264. }
  265. }
  266. private static final long ADDEND = 0xBL;
  267. private static final long MASK = (1L << 48) - 1;
  268. private static final long MULTIPLIER = 0x5DEECE66DL;
  269. private static final RandomContainer rnd = newInstance(RandomType.UNSECURE_THREAD_LOCAL);
  270. protected static volatile long SEED_UNIQUIFIER = 8682522807148012L;
  271. public static final Random directRandom()
  272. {
  273. return rnd.directRandom();
  274. }
  275. /**
  276. * Get a random double number from 0 to 1
  277. * @return A random double number from 0 to 1
  278. * @see com.l2jserver.util.Rnd#nextDouble()
  279. */
  280. public static final double get()
  281. {
  282. return rnd.nextDouble();
  283. }
  284. /**
  285. * Gets a random integer number from 0(inclusive) to n(exclusive)
  286. * @param n The superior limit (exclusive)
  287. * @return A random integer number from 0 to n-1
  288. */
  289. public static final int get(final int n)
  290. {
  291. return rnd.get(n);
  292. }
  293. /**
  294. * Gets a random integer number from min(inclusive) to max(inclusive)
  295. * @param min The minimum value
  296. * @param max The maximum value
  297. * @return A random integer number from min to max
  298. */
  299. public static final int get(final int min, final int max)
  300. {
  301. return rnd.get(min, max);
  302. }
  303. /**
  304. * Gets a random long number from min(inclusive) to max(inclusive)
  305. * @param min The minimum value
  306. * @param max The maximum value
  307. * @return A random long number from min to max
  308. */
  309. public static final long get(final long min, final long max)
  310. {
  311. return rnd.get(min, max);
  312. }
  313. public static final RandomContainer newInstance(final RandomType type)
  314. {
  315. switch (type)
  316. {
  317. case UNSECURE_ATOMIC:
  318. return new RandomContainer(new Random());
  319. case UNSECURE_VOLATILE:
  320. return new RandomContainer(new NonAtomicRandom());
  321. case UNSECURE_THREAD_LOCAL:
  322. return new RandomContainer(new ThreadLocalRandom());
  323. case SECURE:
  324. return new RandomContainer(new SecureRandom());
  325. }
  326. throw new IllegalArgumentException();
  327. }
  328. /**
  329. * Get a random boolean state (true or false)
  330. * @return A random boolean state (true or false)
  331. * @see java.util.Random#nextBoolean()
  332. */
  333. public static final boolean nextBoolean()
  334. {
  335. return rnd.nextBoolean();
  336. }
  337. /**
  338. * Fill the given array with random byte numbers from Byte.MIN_VALUE(inclusive) to Byte.MAX_VALUE(inclusive)
  339. * @param array The array to be filled with random byte numbers
  340. * @see java.util.Random#nextBytes(byte[] bytes)
  341. */
  342. public static final void nextBytes(final byte[] array)
  343. {
  344. rnd.nextBytes(array);
  345. }
  346. /**
  347. * Get a random double number from 0 to 1
  348. * @return A random double number from 0 to 1
  349. * @see java.util.Random#nextDouble()
  350. */
  351. public static final double nextDouble()
  352. {
  353. return rnd.nextDouble();
  354. }
  355. /**
  356. * Get a random float number from 0 to 1
  357. * @return A random integer number from 0 to 1
  358. * @see java.util.Random#nextFloat()
  359. */
  360. public static final float nextFloat()
  361. {
  362. return rnd.nextFloat();
  363. }
  364. /**
  365. * Get a random gaussian double number from 0 to 1
  366. * @return A random gaussian double number from 0 to 1
  367. * @see java.util.Random#nextGaussian()
  368. */
  369. public static final double nextGaussian()
  370. {
  371. return rnd.nextGaussian();
  372. }
  373. /**
  374. * Get a random integer number from Integer.MIN_VALUE(inclusive) to Integer.MAX_VALUE(inclusive)
  375. * @return A random integer number from Integer.MIN_VALUE to Integer.MAX_VALUE
  376. * @see java.util.Random#nextInt()
  377. */
  378. public static final int nextInt()
  379. {
  380. return rnd.nextInt();
  381. }
  382. /**
  383. * @param n
  384. * @return
  385. * @see com.l2jserver.util.Rnd#get(int n)
  386. */
  387. public static final int nextInt(final int n)
  388. {
  389. return get(n);
  390. }
  391. /**
  392. * Get a random long number from Long.MIN_VALUE(inclusive) to Long.MAX_VALUE(inclusive)
  393. * @return A random integer number from Long.MIN_VALUE to Long.MAX_VALUE
  394. * @see java.util.Random#nextLong()
  395. */
  396. public static final long nextLong()
  397. {
  398. return rnd.nextLong();
  399. }
  400. }