Example usage for java.security SecureRandom nextDouble

List of usage examples for java.security SecureRandom nextDouble

Introduction

In this page you can find the example usage for java.security SecureRandom nextDouble.

Prototype

public double nextDouble() 

Source Link

Document

Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

Usage

From source file:com.intel.diceros.test.securerandom.DRNGTest.java

@Override
public void performTest() throws Exception {
    SecureRandom random = SecureRandom.getInstance("DRNG", "DC");
    random.nextDouble();
    byte[] bytes = new byte[65536];
    random.nextBytes(bytes);/*from  ww w .ja va 2 s. c  o  m*/

    final int[] epsilon = bytes2Ints(bytes);
    testFrequency(epsilon);
    testBlockFrequency(epsilon, 10);
    testRuns(epsilon);
    testLongestRunOfOnes(epsilon);
    testRank(epsilon);
    testDiscreteFourierTransform(epsilon);
    testNonOverlappingTemplateMatching(epsilon, 4);
    testOverlappingTemplateMatchings(epsilon, 9);
    testUniversal(epsilon);
    testLinearComplexity(epsilon, 1000);
    testSerial(epsilon, 2);
    testApproximateEntropy(epsilon, 2);
    testCumulativeSums(epsilon);
    testRandomExcursions(epsilon);
    testRandomExcursionsVariant(epsilon);
}

From source file:org.jresponder.util.TokenUtil.java

public String[] generateTokens(int aTokenCount, int aTokenLength) {
    StringBuilder myPasswordBuilder;
    String[] myPasswordArray = new String[aTokenCount];
    int c1;/*from   w w  w.j a v a 2 s  .  co m*/
    int c2;
    int c3;
    long sum = 0;
    int myNChars;
    long myRandomNumber;
    int myPasswordIndex;
    double myThreshold;
    SecureRandom myRandom = new SecureRandom(); // random seed by current time

    /*======================================================================*/
    /* generate the number of passwords requested                           */
    /*======================================================================*/
    for (myPasswordIndex = 0; myPasswordIndex < aTokenCount; myPasswordIndex++) {
        myPasswordBuilder = new StringBuilder(aTokenLength);

        /*==============================================================*/
        /* Pick a random starting point                                 */
        /*==============================================================*/
        myThreshold = myRandom.nextDouble(); // random number [0,1]
        myRandomNumber = (long) (myThreshold * sigma); // weight by sum of frequencies

        /*==============================================================*/
        /* Find the first random pronounceable triplet                  */
        /*==============================================================*/
        sum = 0;
        for (c1 = 0; c1 < 26; c1++) {
            for (c2 = 0; c2 < 26; c2++) {
                for (c3 = 0; c3 < 26; c3++) {
                    sum += (long) (TRIPLET_FREQUENCY[c1][c2][c3]);
                    if (sum > myRandomNumber) {
                        myPasswordBuilder.append(alphabet.charAt(c1));
                        myPasswordBuilder.append(alphabet.charAt(c2));
                        myPasswordBuilder.append(alphabet.charAt(c3));
                        c1 = 26; // Found start - break all 3 loops
                        c2 = 26;
                        c3 = 26;
                    }
                }
            }
        }

        /*==============================================================*/
        /* Now continue based upon the last two characters so far       */
        /*==============================================================*/
        myNChars = 3;
        while (myNChars < aTokenLength) {
            c1 = alphabet.indexOf(myPasswordBuilder.charAt(myNChars - 2));
            c2 = alphabet.indexOf(myPasswordBuilder.charAt(myNChars - 1));
            sum = 0;

            for (c3 = 0; c3 < 26; c3++) {
                sum += (long) (TRIPLET_FREQUENCY[c1][c2][c3]);
            }
            if (sum == 0) {
                break;
            }

            myThreshold = myRandom.nextDouble();
            myRandomNumber = (long) (myThreshold * sum);
            sum = 0;

            for (c3 = 0; c3 < 26; c3++) {
                sum += (long) (TRIPLET_FREQUENCY[c1][c2][c3]);
                if (sum > myRandomNumber) {
                    myPasswordBuilder.append(alphabet.charAt(c3));
                    break;
                }
            }
            myNChars++;
        }

        myPasswordArray[myPasswordIndex] = myPasswordBuilder.toString();
    }

    return (myPasswordArray);
}