Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* DSI utilities
*
* Copyright (C) 2013-2020 Sebastiano Vigna
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see .
*
*/
package it.unimi.dsi.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class XorShift1024StarPhiRandomGeneratorTest {
private final static long seeds[] = { 0, 1, 1024, 0x5555555555555555L };
@Test
public void testNextFloat() {
for (final long seed : seeds) {
final XorShift1024StarPhiRandomGenerator xorShift = new XorShift1024StarPhiRandomGenerator(seed);
double avg = 0;
for (int i = 1000000; i-- != 0;) {
final float d = xorShift.nextFloat();
assertTrue(d < 1);
assertTrue(d >= 0);
avg += d;
}
assertEquals(500000, avg, 500);
}
}
@Test
public void testNextDouble() {
for (final long seed : seeds) {
final XorShift1024StarPhiRandomGenerator xorShift = new XorShift1024StarPhiRandomGenerator(seed);
double avg = 0;
for (int i = 1000000; i-- != 0;) {
final double d = xorShift.nextDouble();
assertTrue(d < 1);
assertTrue(d >= 0);
avg += d;
}
assertEquals(500000, avg, 500);
}
}
@Test
public void testNextDoubleFast() {
for (final long seed : seeds) {
final XorShift1024StarPhiRandomGenerator xorShift = new XorShift1024StarPhiRandomGenerator(seed);
double avg = 0;
for (int i = 1000000; i-- != 0;) {
final double d = xorShift.nextDoubleFast();
assertTrue(d < 1);
assertTrue(d >= 0);
avg += d;
}
assertEquals(500000, avg, 500);
}
}
@Test
public void testNextInt() {
for (final long seed : seeds) {
final XorShift1024StarPhiRandomGenerator xorShift = new XorShift1024StarPhiRandomGenerator(seed);
double avg = 0;
for (int i = 100000000; i-- != 0;) {
final int d = xorShift.nextInt(101);
assertTrue(d <= 100);
assertTrue(d >= 0);
avg += d;
}
assertEquals(5000000000L, avg, 600000);
}
}
@Test
public void testNextInt2() {
for (final long seed : seeds) {
final XorShift1024StarPhiRandomGenerator xorShift = new XorShift1024StarPhiRandomGenerator(seed);
final int[] count = new int[32];
long change = 0;
int prev = 0;
for (int i = 1000000; i-- != 0;) {
final int d = xorShift.nextInt();
change += Long.bitCount(d ^ prev);
for (int b = 32; b-- != 0;)
if ((d & (1 << b)) != 0) count[b]++;
prev = d;
}
assertEquals(32 * 1000000L, change, 60000);
for (int b = 32; b-- != 0;) assertEquals(500000, count[b], 1600);
}
}
@Test
public void testNextInt3() {
final XorShift1024StarPhiRandomGenerator xorShiftRandom = new XorShift1024StarPhiRandomGenerator(0);
for(int i = 0; i < 100; i++) assertTrue(xorShiftRandom.nextInt(16) < 16);
}
@Test
public void testNextInt4() {
final XorShift1024StarPhiRandomGenerator xorShift = new XorShift1024StarPhiRandomGenerator(0);
assertEquals(0, xorShift.nextInt(1));
}
@Test
public void testNextLong() {
for (final long seed : seeds) {
final XorShift1024StarPhiRandomGenerator xorShift = new XorShift1024StarPhiRandomGenerator(seed);
final int[] count = new int[64];
long change = 0;
long prev = 0;
for (int i = 1000000; i-- != 0;) {
final long d = xorShift.nextLong();
change += Long.bitCount(d ^ prev);
for (int b = 64; b-- != 0;)
if ((d & (1L << b)) != 0) count[b]++;
prev = d;
}
assertEquals(32 * 1000000L, change, 10000);
for (int b = 64; b-- != 0;) assertEquals(500000, count[b], 1800);
}
}
@Test
public void testSameAsRandom() {
final XorShift1024StarPhiRandom xorShiftStarRandom = new XorShift1024StarPhiRandom(0);
final XorShift1024StarPhiRandomGenerator xorShiftStar = new XorShift1024StarPhiRandomGenerator(0);
for(int i = 1000000; i-- != 0;) {
assertEquals(xorShiftStar.nextLong(), xorShiftStarRandom.nextLong());
assertEquals(0, xorShiftStar.nextDouble(), xorShiftStarRandom.nextDouble());
assertEquals(xorShiftStar.nextInt(), xorShiftStarRandom.nextInt());
assertEquals(xorShiftStar.nextInt(99), xorShiftStarRandom.nextInt(99));
assertEquals(xorShiftStar.nextInt(128), xorShiftStarRandom.nextInt(128));
}
}
@Test
public void testJump() {
final XorShift1024StarPhiRandomGenerator xorShiftRandom0 = new XorShift1024StarPhiRandomGenerator(0), xorShiftRandom1 = new XorShift1024StarPhiRandomGenerator(0);
xorShiftRandom0.nextLong();
xorShiftRandom0.jump();
xorShiftRandom1.jump();
xorShiftRandom1.nextLong();
assertEquals(xorShiftRandom0.nextLong(), xorShiftRandom1.nextLong());
}
}