/* * Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code 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 General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/ import java.util.ArrayList; import java.util.List; import java.util.PrimitiveIterator;
/** * @test * @summary test bit sequences produced by clases that implement interface RandomGenerator * @bug 8248862 * @run main RandomTestChiSquared * @key randomness
*/
staticvoid fail(String format, Object... args) { if (currentRNG.length() != 0) {
System.err.println(currentRNG);
currentRNG = "";
}
System.err.format(" " + format, args);
failCount++;
}
// Some simple chi-squared tests similar to that used for the FIPS 140 poker test, // but intended primarily to test production of random values from ranges.
// N is the number of categories; every element of s ought to be in [0,N). // N must be in [1,31]. staticboolean chiSquaredTest(String id, int N, IntSupplier theSupplier) { int[] stats = newint[N]; for (int j = 0; j < SEQUENCE_SIZE; j++) { int v = theSupplier.getAsInt(); // assert((0 <= v) && (v < N));
++stats[v];
} int z = 0; for (int k = 0; k < stats.length; k++) {
z += stats[k]*stats[k];
} double x = ((double)N / (double)SEQUENCE_SIZE) * (double)z - (double)SEQUENCE_SIZE; double lo = chiSquaredBounds[N][0]; double hi = chiSquaredBounds[N][1]; boolean chiSquaredSuccess = ((lo < x) && (x < hi)); if (!chiSquaredSuccess) fail("chi-squared test failure for %s: x=%g (should be in [%g,%g])\n", id, x, lo, hi); return chiSquaredSuccess;
}
staticboolean testRngChiSquared(String id, int N, IntSupplier theSupplier) { if (chiSquaredTest(id, N, theSupplier)) returntrue;
fail("testRngChiSquared glitch"); return chiSquaredTest(id, N, theSupplier) && chiSquaredTest(id, N, theSupplier);
}
// To test one instance of an RandomGenerator with the BSI test suite, we test: // nextInt() // nextLong() // nextBoolean() // ints() // longs() // doubles() // nextDouble() // nextFloat()
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.