Rnd.java 11 KB

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