forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLcgRng.java
More file actions
47 lines (37 loc) · 926 Bytes
/
LcgRng.java
File metadata and controls
47 lines (37 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package fj;
/**
* Created by MarkPerry on 7/07/2014.
*
* https://en.wikipedia.org/wiki/Linear_congruential_generator
*/
public class LcgRng extends Rng {
private final Long seed;
public LcgRng() {
this(System.currentTimeMillis());
}
public LcgRng(long s) {
seed = s;
}
public long getSeed() {
return seed;
}
public P2<Rng, Integer> nextInt() {
P2<Rng, Long> p = nextLong();
int i = (int) p._2().longValue();
return P.p(p._1(), i);
}
public P2<Rng, Long> nextLong() {
P2<Long, Long> p = nextLong(seed);
return P.p(new LcgRng(p._1()), p._2());
}
/**
*
* @param seed
* @return Product of Seed and value
*/
static P2<Long, Long> nextLong(long seed) {
long newSeed = (seed * 0x5DEECE66DL + 0xBL) & 0xFFFFFFFFFFFFL;
long n = (Long) (newSeed >>> 16);
return P.p(newSeed, n);
}
}