// Path: prngine/src/main/java/io/jenetics/prngine/Bytes.java
// static int readInt(final byte[] bytes, final int index) {
// 	final int offset = index*Integer.BYTES;
// 	if (offset + Integer.BYTES > bytes.length) {
// 		throw new IndexOutOfBoundsException(format(
// 			"Not enough data to read int value (index=%d, bytes=%d).",
// 			index, bytes.length
// 		));
// 	}
// 
// 	return
// 		((bytes[offset    ] & 255) << 24) +
// 		((bytes[offset + 1] & 255) << 16) +
// 		((bytes[offset + 2] & 255) << 8) +
// 		((bytes[offset + 3] & 255));
// }


import java.util.Random;
import static java.lang.String.format;
import static io.jenetics.prngine.Bytes.readInt;
/*
 * PRNGine - Java PRNG Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
 */
package io.jenetics.prngine;



/**
 * Implementation of an simple PRNG as proposed in
 * <a href="http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf">
 * Good Practice in (Pseudo) Random Number Generation for Bioinformatics
 * Applications</a> (JKISS32, page 3) by <em><a href="mailto:d.jones@cs.ucl.ac.uk">
 * David Jones</a>, UCL Bioinformatics Group</em>.
 * <p>
 * The following listing shows the actual PRNG implementation.
 * <pre>{@code
 * private int x, y, z, w = <seed>
 * private int c = 0;
 *
 * int nextInt() {
 *     int t;
 *     y ^= y << 5;
 *     y ^= y >>> 7;
 *     y ^= y << 22;
 *
 *     t = z + w + c;
 *     z = w;
 *     c = t >>> 31;
 *     w = t & 2147483647;
 *     x += 1411392427;
 *
 *     return x + y + w;
 * }
 * }</pre>
 *
 * <p>
 * The period of this <i>PRNG</i> is &asymp; 2<sup>121</sup>
 * &asymp; 2.6&sdot;10<sup>36</sup>
 *
 * <p>
 * <strong>Not that the base implementation of the {@code KISS32Random}
 * class is not thread-safe.</strong> If multiple threads requests random
 * numbers from this class, it <i>must</i> be synchronized externally.
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
 * @since 1.0
 * @version 2.0.0
 */
public class KISS32Random extends Random32 implements SplittableRandom {

	/**
	 * The internal state of random engine.
	 *
	 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
	 * @since 1.0
	 * @version 2.0.0
	 */
	private static final class State {
		private static final int SEED_BYTES = 16;

		int x, y, z, w, c;

		State(final byte[] seed) {
			if (seed.length < SEED_BYTES) {
				throw new IllegalArgumentException(format(
					"Required %d seed bytes, but got %d.",
					SEED_BYTES, seed.length
				));
			}

// Path: prngine/src/main/java/io/jenetics/prngine/Bytes.java
// static int readInt(final byte[] bytes, final int index) {
// 	final int offset = index*Integer.BYTES;
// 	if (offset + Integer.BYTES > bytes.length) {
// 		throw new IndexOutOfBoundsException(format(
// 			"Not enough data to read int value (index=%d, bytes=%d).",
// 			index, bytes.length
// 		));
// 	}
// 
// 	return
// 		((bytes[offset    ] & 255) << 24) +
// 		((bytes[offset + 1] & 255) << 16) +
// 		((bytes[offset + 2] & 255) << 8) +
// 		((bytes[offset + 3] & 255));
// }


// Path: prngine/src/main/java/io/jenetics/prngine/KISS32Random.java
import java.util.Random;
import static java.lang.String.format;
import static io.jenetics.prngine.Bytes.readInt;

/*
 * PRNGine - Java PRNG Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
 */
package io.jenetics.prngine;



/**
 * Implementation of an simple PRNG as proposed in
 * <a href="http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf">
 * Good Practice in (Pseudo) Random Number Generation for Bioinformatics
 * Applications</a> (JKISS32, page 3) by <em><a href="mailto:d.jones@cs.ucl.ac.uk">
 * David Jones</a>, UCL Bioinformatics Group</em>.
 * <p>
 * The following listing shows the actual PRNG implementation.
 * <pre>{@code
 * private int x, y, z, w = <seed>
 * private int c = 0;
 *
 * int nextInt() {
 *     int t;
 *     y ^= y << 5;
 *     y ^= y >>> 7;
 *     y ^= y << 22;
 *
 *     t = z + w + c;
 *     z = w;
 *     c = t >>> 31;
 *     w = t & 2147483647;
 *     x += 1411392427;
 *
 *     return x + y + w;
 * }
 * }</pre>
 *
 * <p>
 * The period of this <i>PRNG</i> is &asymp; 2<sup>121</sup>
 * &asymp; 2.6&sdot;10<sup>36</sup>
 *
 * <p>
 * <strong>Not that the base implementation of the {@code KISS32Random}
 * class is not thread-safe.</strong> If multiple threads requests random
 * numbers from this class, it <i>must</i> be synchronized externally.
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
 * @since 1.0
 * @version 2.0.0
 */
public class KISS32Random extends Random32 implements SplittableRandom {

	/**
	 * The internal state of random engine.
	 *
	 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
	 * @since 1.0
	 * @version 2.0.0
	 */
	private static final class State {
		private static final int SEED_BYTES = 16;

		int x, y, z, w, c;

		State(final byte[] seed) {
			if (seed.length < SEED_BYTES) {
				throw new IllegalArgumentException(format(
					"Required %d seed bytes, but got %d.",
					SEED_BYTES, seed.length
				));
			}

			x = readInt(seed, 0);