Class Mwc
java.lang.Object
io.github.olyutorskii.aletojio.rng.mwc.Mwc
- All Implemented Interfaces:
RndInt32
- Direct Known Subclasses:
Cmwc
lag-r Multiply-with-carry random number generator.
Random sequence X(n) is derived as follows with parameter a(multiplier), b(divisor).
T(n) = a * X(n-r) + C(n-1)
C(n) = T(n) / b
X(n) = T(n) mod b
r is the length of the record that stores past results. "carry" comes from C(n-1).
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected long
funcCarryFromTval
(long tVal) Function that takes T-value as an argument and returns carry.protected int
funcRndFromTval
(long tVal) Function that takes T-value as an argument and returns random result.protected long
funcTval
(long oldest, long lastCarry) Function that takes oldest result and last carry as arguments and returns T-value.long
getCarry()
Return carry value.protected int
Get oldest result.int
Return length of past records.int
Return next random number as 32bit int with random sign-bit.protected void
pushLatest
(int iVal) Push latest result.void
setCarry
(long lVal) Set carry value.void
setSeeds
(int... seeds) Set seeds.
-
Constructor Details
-
Mwc
Constructor.- record length must be greater than 0.
- multiplier must be greater than 1.
- divisor must be greater than 1.
If you want 32-bit uniformly distributed random numbers, the divisor must be 2**32, but you can compromise with 2**32 -1.
- Parameters:
recLen
- records lengthmulArg
- multiplierdivArg
- divisor- Throws:
IllegalArgumentException
- illegal argument
-
Mwc
Constructor.- record length must be greater than 0.
- multiplier must be greater than 1.
Default divisor value = 2**32.
- Parameters:
recLen
- records lengthmulArg
- multiplier- Throws:
IllegalArgumentException
- illegal argument
-
Mwc
Constructor.- Default record length = 1038
- Default multiplier value = 611373678
- Default divisor value = 2**32
* Caution : If you don't provide your entropy to seeds, it takes over million trials until the seed is scrambled.
- Throws:
IllegalArgumentException
- illegal argument- See Also:
-
-
Method Details
-
getRecords
public int getRecords()Return length of past records.- Returns:
- length of past records
-
getCarry
public long getCarry()Return carry value.- Returns:
- carry value
-
setCarry
public void setCarry(long lVal) Set carry value.- Parameters:
lVal
- new carry value
-
setSeeds
Set seeds.Seeds length must be same as the result records.
Younger index represent older results.
- Parameters:
seeds
- seeds- Throws:
NullPointerException
- argument is nullIllegalArgumentException
- size unmatched
-
getOldest
protected int getOldest()Get oldest result.It's mean X(n-r).
- Returns:
- oldest result
-
pushLatest
protected void pushLatest(int iVal) Push latest result.The oldest result at this time is lost.
- Parameters:
iVal
- latest result
-
funcTval
protected long funcTval(long oldest, long lastCarry) Function that takes oldest result and last carry as arguments and returns T-value.- Parameters:
oldest
- oldest resultlastCarry
- last carry- Returns:
- T-value
-
funcRndFromTval
protected int funcRndFromTval(long tVal) Function that takes T-value as an argument and returns random result.- Parameters:
tVal
- T-value- Returns:
- random result
-
funcCarryFromTval
protected long funcCarryFromTval(long tVal) Function that takes T-value as an argument and returns carry.- Parameters:
tVal
- T-value- Returns:
- carry
-
nextInt32
public int nextInt32()Return next random number as 32bit int with random sign-bit.
-