View Javadoc
1   /*
2    * License : The MIT License
3    * Copyright(c) 2022 olyutorskii
4    */
5   
6   package io.github.olyutorskii.aletojio.rng.lcg;
7   
8   import io.github.olyutorskii.aletojio.rng.RndInt32;
9   
10  /**
11   * Implementation of 32bit output Linear congruential generator(LCG).
12   *
13   * <p>LCG is a commonly used random number generator in the past.
14   *
15   * <p>Recurrence relation sequences : {@code X(n+1) = (X(n) * Mul + Inc) mod Mod}
16   *
17   * <ul>
18   * <li>Mul : Multiplier
19   * <li>Inc : Increment
20   * <li>Mod : Modulus
21   * </ul>
22   *
23   * @see <a href="https://en.wikipedia.org/wiki/Linear_congruential_generator">
24   * Linear congruential generator (Wikipedia)
25   * </a>
26   */
27  public class LcgRndInt32 extends AbstractLcg implements RndInt32 {
28  
29      /**
30       * Constructor.
31       *
32       * <ul>
33       * <li>Multiplier must be 1 or greatrer.
34       * <li>Increment must be 0 or greatrer.
35       * <li>Modulus must be 2 or greater.
36       * </ul>
37       *
38       * @param mulArg multiplier
39       * @param incArg increment
40       * @param modArg modulus
41       * @throws IllegalArgumentException illegal argument
42       */
43      public LcgRndInt32(long mulArg, long incArg, long modArg)
44              throws IllegalArgumentException {
45          super(mulArg, incArg, modArg);
46          return;
47      }
48  
49      /**
50       * {@inheritDoc}
51       *
52       * <p>result(32bit) reflects seed[16:47]
53       *
54       * @return {@inheritDoc}
55       */
56      @Override
57      protected int seedToResult() {
58          long lVal = getSeed() >>> 16;
59          lVal &= MASK_B32;
60          int result = (int) lVal;
61          return result;
62      }
63  
64      /**
65       * {@inheritDoc}
66       *
67       * @return {@inheritDoc}
68       */
69      @Override
70      public int nextInt32() {
71          int result = nextIntImpl();
72          return result;
73      }
74  
75  }