Rnd.java 12 KB

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