staticvoid fail(String format, Object... args) { if (currentRNG.length() != 0) {
System.err.println(currentRNG);
currentRNG = "";
}
System.err.format(" " + format, args);
failCount++;
}
privatestaticfinalint SEQUENCE_SIZE = 20000;
/* The Monobit Test * *1.Countthenumberofonesinthe20,000bitstream.DenotethisquantitybyX. * *2.Thetestispassedif9,654<X<10,346.
*/ staticint monobitTest(String id, byte[] s) { // System.out.println("monobit test"); int count = 0; for (int j = 0; j < s.length; j++) {
count += s[j];
} int monobitFailure = ((9654 < count) && (count < 10346)) ? 0 : 1; if (monobitFailure != 0) fail("monobit test failure for %s: count=%d (should be in [9654,10346])\n", id, count); return monobitFailure;
}
/* The Poker Test * *1.Dividethe20,000bitstreaminto5,000contiguous4-bitsegments.Countand *storethenumberofoccurrencesofeachofthe16possible4-bitvalues.Denote *f(i)asthenumberofeach4-bitvalueiwhere0<=i<=15. * *2.Evaluatethefollowing:X=(16/5000)(sum[i=0,15](f(i))**2)-5000 * *3.Thetestispassedif1.03<X<57.4.
*/
staticint pokerTest(String id, byte[] s) { // System.out.println("poker test"); // Divide the bit sequence into 4-bit chunks, and count the number of times each 4-bit value appears. int[] stats = newint[16]; int v = 0; for (int j = 0; j < s.length; j++) {
v = (v << 1) | s[j]; if ((j & 3) == 3) {
++stats[v];
v = 0;
}
} int z = 0; for (int k = 0; k < stats.length; k++) {
z += stats[k]*stats[k];
} double x = (16.0 / (s.length / 4)) * z - (s.length / 4); int pokerFailure = ((1.03 < x) && (x < 57.4)) ? 0 : 1; if (pokerFailure != 0) fail("poker test failure for %s: x=%g (should be in [1.03,57.4])\n", id, x); return pokerFailure;
}
/* The Runs Test * *1.Arunisdefinedasamaximalsequenceofconsecutivebitsofeitherallones *orallzeros,whichispartofthe20,000bitsamplestream.Theincidencesof *runs(forbothconsecutivezerosandconsecutiveones)ofalllengths(>=1)in *thesamplestreamshouldbecountedandstored. * *2.Thetestispassedifthenumberofrunsthatoccur(oflengths1through6) *iseachwithinthecorrespondingintervalspecifiedbelow.Thismustholdfor *boththezerosandones;thatis,all12countsmustlieinthespecified *interval.Forthepurposeofthistest,runsofgreaterthan6areconsideredto *beoflength6. * *LengthofrunRequiredInterval *12,267-2,733 *21,079-1,421 *3502-748 *4223-402 *590-223 *6+90-223 * *TheLongRunTest * *1.Alongrunisdefinedtobearunoflength34ormore(ofeitherzerosorones). * *2.Onthesampleof20,000bits,thetestispassedifthereareNOlongruns.
*/ staticint runTestAndLongRunTest(String id, byte[] s) { // System.out.println("run test"); int[][] stats = newint[2][8]; int count = 0; for (int j = 0; j < s.length; j++) {
++count; if ((j == (s.length - 1)) || (s[j+1] != s[j])) {
++stats[s[j]][(count < 6) ? count : (count < 34) ? 6 : 7];
count = 0;
}
}
stats[0][6] += stats[0][7];
stats[1][6] += stats[1][7]; int runFailure = checkRunStats(stats[0]) | checkRunStats(stats[1]); if (runFailure != 0) fail("run test failure for %s\n", id); int longRunFailure = ((stats[0][7] == 0) && (stats[1][7] == 0)) ? 0 : 1; if (longRunFailure != 0) fail("long run test failure for %s\n", id); return (runFailure + longRunFailure);
}
/* To test a sequence of boolean values from a BooleanSupplier, *sequentiallyextract20000booleanvalues,converttoanarray *ofbytes,andfeedthemtomethod{@codeentireBsi1999Test}.
*/
staticint testRngBsi1999BooleanOnce(String id, BooleanSupplier theSupplier) { int failureCount = 0; byte[] s = newbyte[SEQUENCE_SIZE]; // Take the next SEQUENCE_SIZE booleans and test them for (int j = 0; j < s.length; j++) {
s[j] = (theSupplier.getAsBoolean() ? (byte)1 : (byte)0);
}
failureCount += entireBsi1999Test(id + " consecutive", s); return failureCount;
}
/* To test a sequence of long values from a LongSupplier, *twokindsoftestsareperformed. * *Thefirstkindoftestextracts313=ceiling(20000/64)long *valuesandconcatenatesalltheirbits;thefirst20000bits *areconvertedtoabytearrayofbitstobetested.Thistestis *repeated64times. * *Thesecondkindoftestfocusesononebitpositionm(0<=m<64); *itextracts20000longvaluesandusesjustbitmfromeachvalue *toproduceanarrayofbytestobetested.Thistestisperformed *onceforeachpossiblevalueofm(64timesinall).
*/ staticint testRngBsi1999LongOnce(String id, LongSupplier theSupplier) { int failureCount = 0; byte[] s = newbyte[SEQUENCE_SIZE]; // Part 1: 64 times, take the next SEQUENCE_SIZE bits and test them for (int m = 0; m < 64; m++) { long bits = 0; int bitCount = 0; for (int j = 0; j < s.length; j++) { if ((j & 0x3f) == 0) {
bits = theSupplier.getAsLong(); // System.out.printf("0x%016x\n", bits);
bitCount += Long.bitCount((j == (20000 - 32)) ? ((bits << 32) >>> 32) : bits);
}
s[j] = (byte)(bits & 1);
bits >>>= 1;
} // System.out.println(m + ": " + bitCount + " 1-bits");
failureCount += entireBsi1999Test(id + " consecutive (" + bitCount + " 1-bits)", s);
} // Part 2: for 0 <= m < 64, use bit m from each of the next SEQUENCE_SIZE longs for (int m = 0; m < 64; m++) { for (int j = 0; j < s.length; j++) {
s[j] = (byte)((theSupplier.getAsLong() >>> m) & 1);
}
failureCount += entireBsi1999Test(id + " bit " + m, s);
} return failureCount;
}
/* To test a sequence of ing values from an IntSupplier, *twokindsoftestsareperformed. * *Thefirstkindoftestextracts625=20000/32intvaluesand *concatenatesalltheirbits;these20000bitsareconvertedto *abytearrayofbitstobetested.Thistestisrepeated64 *times. * *Thesecondkindoftestfocusesononebitpositionm(0<=m<32); *itextracts20000intvaluesandusesjustbitmfromeachvalue *toproduceanarrayofbytestobetested.Thistestisperformed *onceforeachpossiblevalueofm(32timesinall).
*/ staticint testRngBsi1999IntOnce(String id, IntSupplier theSupplier) { int failureCount = 0; byte[] s = newbyte[SEQUENCE_SIZE]; // Part 1: 64 times, take the next SEQUENCE_SIZE bits and test them for (int m = 0; m < 64; m++) { int bits = 0; int bitCount = 0; for (int j = 0; j < s.length; j++) { if ((j & 0x1f) == 0) {
bits = theSupplier.getAsInt();
bitCount += Integer.bitCount(bits);
}
s[j] = (byte)(bits & 1);
bits >>>= 1;
} // System.out.println(m + ": " + bitCount + " 1-bits");
failureCount += entireBsi1999Test(id + " consecutive (" + bitCount + " 1-bits)", s);
} // Part 2: for 0 <= m < 32, use bit m from each of the next SEQUENCE_SIZE ints for (int m = 0; m < 32; m++) { for (int j = 0; j < s.length; j++) {
s[j] = (byte)((theSupplier.getAsInt() >>> m) & 1);
}
failureCount += entireBsi1999Test(id + " bit " + m, s);
} return failureCount;
}
/* A call to {@code entireBsi1999Test} may report failure even if the source of random *bitsisquitegood,becausethetestisstatisticalinnature.Tomakethetesting *proceduremorerobust,ifthefirstcallto{@codeentireBsi1999Test}fails,then *thefailureisignorediftwomorecallsto{@codeentireBsi1999Test}bothsucceed.
*/
if (currentRNG.equals("SecureRandom")) { // skip because stochastic
} elseif (currentRNG.equals("Random")) { // testOneRng(factory.create(59), 1); // autocorrelation failure for java.util.Random longs bit 0: count=2207 (should be in [2267,2733]), tau=2819
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 und die Messung sind noch experimentell.