Commit d39a7e39 authored by pysaumont's avatar pysaumont
Browse files

Fixed random generator

parent 78cf3d69
package com.fpinjava.state.exercise12_01;
import com.fpinjava.common.Function;
import com.fpinjava.common.List;
import com.fpinjava.common.Result;
import com.fpinjava.common.Tuple;
public class Generator {
......
......@@ -2,27 +2,29 @@ package com.fpinjava.state.exercise12_01;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
......
......@@ -2,27 +2,30 @@ package com.fpinjava.state.exercise12_02;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
......
......@@ -2,30 +2,34 @@ package com.fpinjava.state.exercise12_03;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
......@@ -2,30 +2,34 @@ package com.fpinjava.state.exercise12_04;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
......@@ -2,30 +2,34 @@ package com.fpinjava.state.exercise12_05;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
......@@ -2,30 +2,34 @@ package com.fpinjava.state.exercise12_06;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
......@@ -2,30 +2,33 @@ package com.fpinjava.state.exercise12_07;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
}
\ No newline at end of file
......@@ -2,30 +2,33 @@ package com.fpinjava.state.exercise12_08;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
}
\ No newline at end of file
......@@ -2,30 +2,34 @@ package com.fpinjava.state.exercise12_09;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
......@@ -10,25 +10,42 @@ public class GeneratorTest {
@Test
public void testInteger() {
// RNG rng = JavaRNG.rng(0);
// Tuple<Integer, RNG> t1 = Generator.integer(rng);
// assertEquals(Integer.valueOf(-1155484576), t1._1);
// Tuple<Integer, RNG> t2 = Generator.integer(t1._2);
// assertEquals(Integer.valueOf(-723955400), t2._1);
// Tuple<Integer, RNG> t3 = Generator.integer(t2._2);
// assertEquals(Integer.valueOf(1033096058), t3._1);
// Tuple<Integer, RNG> t4 = Generator.integer(t3._2);
// assertEquals(Integer.valueOf(-1690734402), t4._1);
RNG rng = JavaRNG.rng(0);
Tuple<Integer, RNG> t1 = Generator.integer(rng);
assertEquals(Integer.valueOf(-1155484576), t1._1);
assertEquals(Integer.valueOf(384748), t1._1);
Tuple<Integer, RNG> t2 = Generator.integer(t1._2);
assertEquals(Integer.valueOf(-723955400), t2._1);
assertEquals(Integer.valueOf(-1155484576), t2._1);
Tuple<Integer, RNG> t3 = Generator.integer(t2._2);
assertEquals(Integer.valueOf(1033096058), t3._1);
assertEquals(Integer.valueOf(-723955400), t3._1);
Tuple<Integer, RNG> t4 = Generator.integer(t3._2);
assertEquals(Integer.valueOf(-1690734402), t4._1);
assertEquals(Integer.valueOf(1033096058), t4._1);
}
@Test
public void testIntegerRt() {
RNG rng = JavaRNG.rng(0);
assertEquals(Integer.valueOf(384748), Generator.integer(rng)._1);
assertEquals(Integer.valueOf(384748), Generator.integer(rng)._1);
assertEquals(Integer.valueOf(384748), Generator.integer(rng)._1);
}
@Test
public void testIntegerLimit() {
RNG rng = JavaRNG.rng(0);
Tuple<Integer, RNG> t1 = Generator.integer(rng, 100);
assertEquals(Integer.valueOf(76), t1._1);
assertEquals(Integer.valueOf(48), t1._1);
Tuple<Integer, RNG> t2 = Generator.integer(t1._2, 100);
assertEquals(Integer.valueOf(0), t2._1);
assertEquals(Integer.valueOf(76), t2._1);
Tuple<Integer, RNG> t3 = Generator.integer(t2._2, 100);
assertEquals(Integer.valueOf(58), t3._1);
assertEquals(Integer.valueOf(0), t3._1);
}
}
......@@ -14,18 +14,18 @@ public class GeneratorTest {
public void testIntegers() {
RNG rng = JavaRNG.rng(0);
Tuple<List<Integer>, RNG> result = Generator.integers(rng, 3);
assertEquals(List.list(1033096058, -723955400, -1155484576), result._1);
assertEquals(List.list(-723955400, -1155484576, 384748), result._1);
Tuple<Integer, RNG> t = Generator.integer(result._2);
assertEquals(Integer.valueOf(-1690734402), t._1);
assertEquals(Integer.valueOf(1033096058), t._1);
}
@Test
public void testIntegersLength1() {
RNG rng = JavaRNG.rng(0);
Tuple<List<Integer>, RNG> result = Generator.integers(rng, 1);
assertEquals(List.list(-1155484576), result._1);
assertEquals(List.list(384748), result._1);
Tuple<Integer, RNG> t = Generator.integer(result._2);
assertEquals(Integer.valueOf(-723955400), t._1);
assertEquals(Integer.valueOf(-1155484576), t._1);
}
@Test
......@@ -34,7 +34,7 @@ public class GeneratorTest {
Tuple<List<Integer>, RNG> result = Generator.integers(rng, 0);
assertEquals(0, result._1.length());
Tuple<Integer, RNG> t = Generator.integer(result._2);
assertEquals(Integer.valueOf(-1155484576), t._1);
assertEquals(Integer.valueOf(384748), t._1);
}
@Test
......@@ -43,25 +43,25 @@ public class GeneratorTest {
Tuple<List<Integer>, RNG> result = Generator.integers(rng, -3);
assertEquals(0, result._1.length());
Tuple<Integer, RNG> t = Generator.integer(result._2);
assertEquals(Integer.valueOf(-1155484576), t._1);
assertEquals(Integer.valueOf(384748), t._1);
}
@Test
public void testIntegers2() {
RNG rng = JavaRNG.rng(0);
Tuple<List<Integer>, RNG> result = Generator.integers2(rng, 3);
assertEquals(List.list(-1155484576, -723955400, 1033096058), result._1);
assertEquals(List.list(384748, 384748, 384748), result._1);
Tuple<Integer, RNG> t = Generator.integer(result._2);
assertEquals(Integer.valueOf(-1690734402), t._1);
assertEquals(Integer.valueOf(-1155484576), t._1);
}
@Test
public void testIntegers2Length1() {
RNG rng = JavaRNG.rng(0);
Tuple<List<Integer>, RNG> result = Generator.integers2(rng, 1);
assertEquals(List.list(-1155484576), result._1);
assertEquals(List.list(384748), result._1);
Tuple<Integer, RNG> t = Generator.integer(result._2);
assertEquals(Integer.valueOf(-723955400), t._1);
assertEquals(Integer.valueOf(-1155484576), t._1);
}
@Test
......@@ -70,7 +70,7 @@ public class GeneratorTest {
Tuple<List<Integer>, RNG> result = Generator.integers2(rng, 0);
assertEquals(0, result._1.length());
Tuple<Integer, RNG> t = Generator.integer(result._2);
assertEquals(Integer.valueOf(-1155484576), t._1);
assertEquals(Integer.valueOf(384748), t._1);
}
@Test
......@@ -79,25 +79,25 @@ public class GeneratorTest {
Tuple<List<Integer>, RNG> result = Generator.integers2(rng, -3);
assertEquals(0, result._1.length());
Tuple<Integer, RNG> t = Generator.integer(result._2);
assertEquals(Integer.valueOf(-1155484576), t._1);
assertEquals(Integer.valueOf(384748), t._1);
}
@Test
public void testIntegers3() {
RNG rng = JavaRNG.rng(0);
Tuple<List<Integer>, RNG> result = Generator.integers3(rng, 3);
assertEquals(List.list(1033096058, -723955400, -1155484576), result._1);
assertEquals(List.list(-723955400, -1155484576, 384748), result._1);
Tuple<Integer, RNG> t = Generator.integer(result._2);
assertEquals(Integer.valueOf(-1690734402), t._1);
assertEquals(Integer.valueOf(1033096058), t._1);
}
@Test
public void testIntegers3Length1() {
RNG rng = JavaRNG.rng(0);
Tuple<List<Integer>, RNG> result = Generator.integers3(rng, 1);
assertEquals(List.list(-1155484576), result._1);
assertEquals(List.list(384748), result._1);
Tuple<Integer, RNG> t = Generator.integer(result._2);
assertEquals(Integer.valueOf(-723955400), t._1);
assertEquals(Integer.valueOf(-1155484576), t._1);
}
@Test
......@@ -106,7 +106,7 @@ public class GeneratorTest {
Tuple<List<Integer>, RNG> result = Generator.integers3(rng, 0);
assertEquals(0, result._1.length());
Tuple<Integer, RNG> t = Generator.integer(result._2);
assertEquals(Integer.valueOf(-1155484576), t._1);
assertEquals(Integer.valueOf(384748), t._1);
}
@Test
......@@ -115,7 +115,7 @@ public class GeneratorTest {