/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * 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 2.1 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author:
 *     Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 *
 */
package org.jenetics.util.performance;

import java.text.NumberFormat;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import org.jenetics.util.LCG64ShiftRandom;
//import org.jenetics.util.XORShiftRandom;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @version <em>$Date: 2012-11-30 $</em>
 */
public class RandomPerformanceTests {


	public static void main(final String[] args) {
		final int loops = 500_000_000;

		test(new Random(), loops);
		test(ThreadLocalRandom.current(), loops);
		//test(new XORShiftRandom(), loops);
		test(new LCG64ShiftRandom(), loops);
	}

	private static void test(final Random random, final int loops) {
		System.out.println(String.format(
			"%-20s %s; %s; %s; %s",
			random.getClass().getSimpleName() + ":",
			testNextInt(random, loops),
			testNextLong(random, loops),
			testNextFloat(random, loops),
			testNextDouble(random, loops)
		));
	}

	private static String testNextInt(final Random random, final long loops) {
		long start = System.nanoTime();
		for (long i = 0; i < loops; ++i) {
			random.nextInt();
		}
		long end = System.nanoTime();

		final NumberFormat format = NumberFormat.getIntegerInstance();
		return String.format("%11s i/sec", format.format(perSec(loops, start, end)));
	}

	private static String testNextLong(final Random random, final long loops) {
		long start = System.nanoTime();
		for (long i = 0; i < loops; ++i) {
			random.nextLong();
		}
		long end = System.nanoTime();

		final NumberFormat format = NumberFormat.getIntegerInstance();
		return String.format("%11s l/sec", format.format(perSec(loops, start, end)));
	}

	private static String testNextFloat(final Random random, final long loops) {
		long start = System.nanoTime();
		for (long i = 0; i < loops; ++i) {
			random.nextFloat();
		}
		long end = System.nanoTime();

		final NumberFormat format = NumberFormat.getIntegerInstance();
		return String.format("%11s f/sec", format.format(perSec(loops, start, end)));
	}

	private static String testNextDouble(final Random random, final long loops) {
		long start = System.nanoTime();
		for (long i = 0; i < loops; ++i) {
			random.nextDouble();
		}
		long end = System.nanoTime();

		final NumberFormat format = NumberFormat.getIntegerInstance();
		return String.format("%11s d/sec", format.format(perSec(loops, start, end)));
	}

	private static long perSec(final long loops, final long start, final long end) {
		final double seconds = (end - start)/1_000_000_000.0;
		return (long)(loops/seconds);
	}

}

@@ -1266,15 +1266,15 @@
  201
-2-11-30
+3-07-29
  $%3C/
@@ -1389,17 +1389,39 @@
 loops = 
-5
+Integer.MAX_VALUE;//1_0
 00_000_0
@@ -1946,36 +1946,35 @@
 m random, final 
-long
+int
  loops) %7B%0A%09%09long
@@ -2012,34 +2012,32 @@
 or (
-long
+int
  i = 
-0; i %3C loops; ++i
+loops; --i %3E= 0;
 ) %7B%0A
@@ -2298,36 +2298,35 @@
 m random, final 
-long
+int
  loops) %7B%0A%09%09long
@@ -2364,34 +2364,32 @@
 or (
-long
+int
  i = 
-0; i %3C loops; ++i
+loops; --i %3E= 0;
 ) %7B%0A
@@ -2652,36 +2652,35 @@
 m random, final 
-long
+int
  loops) %7B%0A%09%09long
@@ -2718,34 +2718,32 @@
 or (
-long
+int
  i = 
-0; i %3C loops; ++i
+loops; --i %3E= 0;
 ) %7B%0A
@@ -3012,28 +3012,27 @@
 ndom, final 
-long
+int
  loops) %7B%0A%09%09
@@ -3074,34 +3074,32 @@
 or (
-long
+int
  i = 
-0; i %3C loops; ++i
+loops; --i %3E= 0;
 ) %7B%0A

Increase test loops.