• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Simple Pseudo Random Algorithm dig090  Sep 11th, 2013 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. package com.j256;
2.
3. /**
4.  * Minimal Standard Pseudo-Random Number Generator
5.  *
6.  * Author: Fuat C. Baran, Columbia University, 1988
7.  *
8.  * Based on code in "Random Number Generators: Good Ones are Hard to Find", by Stephen K. Park and Keith W. Miller in
9.  * Communications of the ACM, 31, 10 (Oct. 1988) pp. 1192-1201.
10.  *
11.  * Requirements: MAXINT must be 2 ^ 31 - 1 or larger.
12.  *
13.  * This is needed so we can duplicate a random number algorithm in perl so external entities can run
14.  */
15. public class SimpleRandom {
16.
17.         /* random number generator doesn't handle 0 */
18.         private static final int ZERO_SEED_REPLACEMENT = 0x7EADBEEF;
19.
20.         /* magic number */
21.         private static final int MAGIC_A = 16807;
22.         /* mersenne prime 2^31 -1 */
23.         private static final int MERSENNE_PRIME = 2147483647;
24.         /* M div A (M / A) */
25.         private static final int MAGIC_QUOTIENT = 127773;
26.         /* M mod A (M % A) */
27.         private static final int MAGIC_REMAINDER = 2836;
28.
29.         private int value = 0;
30.
31.         /**
32.          * Creates our generator with an internal seed.
33.          */
34.         public SimpleRandom() {
35.                 while (value == 0) {
36.                         value = (int) System.currentTimeMillis() ^ ZERO_SEED_REPLACEMENT;
37.                 }
38.         }
39.
40.         /**
41.          * Seeds the random number generator.
42.          */
43.         public SimpleRandom(int seed) {
44.                 seed(seed);
45.         }
46.
47.         /**
48.          * Seed the random number generator with the user argument.
49.          */
50.         public void seed(int seed) {
51.                 value = seed;
52.                 resetSeed();
53.         }
54.
55.         /*
56.          * Get a pseudo-random number from the random algorithm.
57.          */
58.         public int generate() {
59.                 /* do the magic seed calculation */
60.                 value = (MAGIC_A * (value % MAGIC_QUOTIENT)) - (MAGIC_REMAINDER * (value / MAGIC_QUOTIENT));
61.                 resetSeed();
62.                 return value;
63.         }
64.
65.         /*
66.          * Get a pseudo-random number from the random algorithm with values between 0 and (maxValue-1).
67.          */
68.         public int generate(int maxValue) {
69.                 if (maxValue == 0) {
70.                         return 0;
71.                 } else {
72.                         return generate() % maxValue;
73.                 }
74.         }
75.
76.         private void resetSeed() {
77.                 if (value < 0) {
78.                         value += MERSENNE_PRIME;
79.                 } else if (value == 0) {
80.                         value = ZERO_SEED_REPLACEMENT;
81.                 }
82.         }
83. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!

Top