Java Code Examples for java.security.SecureRandom#nextDouble()

The following examples show how to use java.security.SecureRandom#nextDouble() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SecureRandomDemo.java    From tutorials with MIT License 6 votes vote down vote up
public static void generateSecureRandomValues() {
    SecureRandom sr = new SecureRandom();

    int randomInt = sr.nextInt();
    long randomLong = sr.nextLong();
    float randomFloat = sr.nextFloat();
    double randomDouble = sr.nextDouble();
    boolean randomBoolean = sr.nextBoolean();

    IntStream randomIntStream = sr.ints();
    LongStream randomLongStream = sr.longs();
    DoubleStream randomDoubleStream = sr.doubles();

    byte[] values = new byte[124];
    sr.nextBytes(values);
}
 
Example 2
Source File: SecureRandomDemo.java    From tutorials with MIT License 6 votes vote down vote up
public static void generateSecureRandomValues() {
    SecureRandom sr = new SecureRandom();

    int randomInt = sr.nextInt();
    long randomLong = sr.nextLong();
    float randomFloat = sr.nextFloat();
    double randomDouble = sr.nextDouble();
    boolean randomBoolean = sr.nextBoolean();

    IntStream randomIntStream = sr.ints();
    LongStream randomLongStream = sr.longs();
    DoubleStream randomDoubleStream = sr.doubles();

    byte[] values = new byte[124];
    sr.nextBytes(values);
}
 
Example 3
Source File: SecureRandom.java    From CompetitiveJava with MIT License 6 votes vote down vote up
public static void generateSecureRandomValues() {
    SecureRandom secureRandom = new SecureRandom();

    int randomInt = secureRandom.nextInt();
    long randomLong = secureRandom.nextLong();
    float randomFloat = secureRandom.nextFloat();
    double randomDouble = secureRandom.nextDouble();
    boolean randomBoolean = secureRandom.nextBoolean();

    IntStream randomIntStream = secureRandom.ints();
    LongStream randomLongStream = secureRandom.longs();
    DoubleStream randomDoubleStream = secureRandom.doubles();

    byte[] values = new byte[124];
    secureRandom.nextBytes(values);
}
 
Example 4
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**  {@inheritDoc} */
public int nextSecureInt(int lower, int upper) {
    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }
    SecureRandom sec = getSecRan();
    double r = sec.nextDouble();
    double scaled = r * upper + (1.0 - r) * lower + r;
    return (int)FastMath.floor(scaled);
}
 
Example 5
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public long nextSecureLong(long lower, long upper) {

    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }
    SecureRandom sec = getSecRan();
    double r = sec.nextDouble();
    double scaled = r * upper + (1.0 - r) * lower + r;
    return (long)FastMath.floor(scaled);
}
 
Example 6
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**  {@inheritDoc} */
public int nextSecureInt(int lower, int upper) {
    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }
    SecureRandom sec = getSecRan();
    double r = sec.nextDouble();
    double scaled = r * upper + (1.0 - r) * lower + r;
    return (int)FastMath.floor(scaled);
}
 
Example 7
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public long nextSecureLong(long lower, long upper) throws NumberIsTooLargeException {
    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }
    SecureRandom sec = getSecRan();
    final double r = sec.nextDouble();
    final double scaled = r * upper + (1.0 - r) * lower + r;
    return (long)FastMath.floor(scaled);
}
 
Example 8
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random long value uniformly distributed between
 * <code>lower</code> and <code>upper</code>, inclusive. This algorithm uses
 * a secure random number generator.
 *
 * @param lower
 *            the lower bound.
 * @param upper
 *            the upper bound.
 * @return the random integer.
 */
public long nextSecureLong(long lower, long upper) {
    if (lower >= upper) {
        throw MathRuntimeException.createIllegalArgumentException(
              "upper bound ({0}) must be greater than lower bound ({1})",
              upper, lower);
    }
    SecureRandom sec = getSecRan();
    return lower + (long) (sec.nextDouble() * (upper - lower + 1));
}
 
Example 9
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random int value uniformly distributed between
 * <code>lower</code> and <code>upper</code>, inclusive. This algorithm uses
 * a secure random number generator.
 *
 * @param lower
 *            the lower bound.
 * @param upper
 *            the upper bound.
 * @return the random integer.
 */
public int nextSecureInt(int lower, int upper) {
    if (lower >= upper) {
        throw MathRuntimeException.createIllegalArgumentException(
              "upper bound ({0}) must be greater than lower bound ({1})",
              upper, lower);
    }
    SecureRandom sec = getSecRan();
    return lower + (int) (sec.nextDouble() * (upper - lower + 1));
}
 
Example 10
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random long value uniformly distributed between
 * <code>lower</code> and <code>upper</code>, inclusive. This algorithm uses
 * a secure random number generator.
 * 
 * @param lower
 *            the lower bound.
 * @param upper
 *            the upper bound.
 * @return the random integer.
 */
public long nextSecureLong(long lower, long upper) {
    if (lower >= upper) {
        throw MathRuntimeException.createIllegalArgumentException(
              "upper bound ({0}) must be greater than lower bound ({1})",
              upper, lower);
    }
    SecureRandom sec = getSecRan();
    return lower + (long) (sec.nextDouble() * (upper - lower + 1));
}
 
Example 11
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random int value uniformly distributed between
 * <code>lower</code> and <code>upper</code>, inclusive. This algorithm uses
 * a secure random number generator.
 *
 * @param lower
 *            the lower bound.
 * @param upper
 *            the upper bound.
 * @return the random integer.
 */
public int nextSecureInt(int lower, int upper) {
    if (lower >= upper) {
        throw MathRuntimeException.createIllegalArgumentException(
              "upper bound ({0}) must be greater than lower bound ({1})",
              upper, lower);
    }
    SecureRandom sec = getSecRan();
    return lower + (int) (sec.nextDouble() * (upper - lower + 1));
}
 
Example 12
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random long value uniformly distributed between
 * <code>lower</code> and <code>upper</code>, inclusive. This algorithm uses
 * a secure random number generator.
 *
 * @param lower
 *            the lower bound.
 * @param upper
 *            the upper bound.
 * @return the random integer.
 * @throws NumberIsTooLargeException if {@code lower >= upper}.
 */
public long nextSecureLong(long lower, long upper) {
    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }
    SecureRandom sec = getSecRan();
    return lower + (long) (sec.nextDouble() * (upper - lower + 1));
}
 
Example 13
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random int value uniformly distributed between
 * <code>lower</code> and <code>upper</code>, inclusive. This algorithm uses
 * a secure random number generator.
 *
 * @param lower
 *            the lower bound.
 * @param upper
 *            the upper bound.
 * @return the random integer.
 */
public int nextSecureInt(int lower, int upper) {
    if (lower >= upper) {
        throw MathRuntimeException.createIllegalArgumentException(
              "upper bound ({0}) must be greater than lower bound ({1})",
              upper, lower);
    }
    SecureRandom sec = getSecRan();
    return lower + (int) (sec.nextDouble() * (upper - lower + 1));
}
 
Example 14
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random long value uniformly distributed between
 * <code>lower</code> and <code>upper</code>, inclusive. This algorithm uses
 * a secure random number generator.
 *
 * @param lower
 *            the lower bound.
 * @param upper
 *            the upper bound.
 * @return the random integer.
 */
public long nextSecureLong(long lower, long upper) {
    if (lower >= upper) {
        throw MathRuntimeException.createIllegalArgumentException(
              "upper bound ({0}) must be greater than lower bound ({1})",
              upper, lower);
    }
    SecureRandom sec = getSecRan();
    return lower + (long) (sec.nextDouble() * (upper - lower + 1));
}
 
Example 15
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random int value uniformly distributed between
 * <code>lower</code> and <code>upper</code>, inclusive. This algorithm uses
 * a secure random number generator.
 * 
 * @param lower
 *            the lower bound.
 * @param upper
 *            the upper bound.
 * @return the random integer.
 */
public int nextSecureInt(int lower, int upper) {
    if (lower >= upper) {
        throw MathRuntimeException.createIllegalArgumentException(
              "upper bound ({0}) must be greater than lower bound ({1})",
              upper, lower);
    }
    SecureRandom sec = getSecRan();
    return lower + (int) (sec.nextDouble() * (upper - lower + 1));
}
 
Example 16
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random int value uniformly distributed between
 * <code>lower</code> and <code>upper</code>, inclusive. This algorithm uses
 * a secure random number generator.
 *
 * @param lower
 *            the lower bound.
 * @param upper
 *            the upper bound.
 * @return the random integer.
 * @throws NumberIsTooLargeException if {@code lower >= upper}.
 */
public int nextSecureInt(int lower, int upper) {
    if (lower >= upper) {
        throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
                                            lower, upper, false);
    }
    SecureRandom sec = getSecRan();
    return lower + (int) (sec.nextDouble() * (upper - lower + 1));
}
 
Example 17
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random int value uniformly distributed between
 * <code>lower</code> and <code>upper</code>, inclusive. This algorithm uses
 * a secure random number generator.
 *
 * @param lower
 *            the lower bound.
 * @param upper
 *            the upper bound.
 * @return the random integer.
 */
public int nextSecureInt(int lower, int upper) {
    if (lower >= upper) {
        throw MathRuntimeException.createIllegalArgumentException(
              "upper bound ({0}) must be greater than lower bound ({1})",
              upper, lower);
    }
    SecureRandom sec = getSecRan();
    return lower + (int) (sec.nextDouble() * (upper - lower + 1));
}
 
Example 18
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random int value uniformly distributed between
 * <code>lower</code> and <code>upper</code>, inclusive. This algorithm uses
 * a secure random number generator.
 * 
 * @param lower
 *            the lower bound.
 * @param upper
 *            the upper bound.
 * @return the random integer.
 */
public int nextSecureInt(int lower, int upper) {
    if (lower >= upper) {
        throw MathRuntimeException.createIllegalArgumentException(
              "upper bound ({0}) must be greater than lower bound ({1})",
              upper, lower);
    }
    SecureRandom sec = getSecRan();
    return lower + (int) (sec.nextDouble() * (upper - lower + 1));
}
 
Example 19
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random int value uniformly distributed between
 * <code>lower</code> and <code>upper</code>, inclusive.  This algorithm
 * uses a secure random number generator.
 * 
 * @param lower the lower bound.
 * @param upper the upper bound.
 * @return the random integer.
 */
public int nextSecureInt(int lower, int upper) {
      if (lower >= upper) {
          throw new IllegalArgumentException
            ("lower bound must be < upper bound");
      }
      SecureRandom sec = getSecRan();
      return lower + (int) (sec.nextDouble() * (upper - lower + 1));
}
 
Example 20
Source File: RandomDataImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Generate a random int value uniformly distributed between
 * <code>lower</code> and <code>upper</code>, inclusive.  This algorithm
 * uses a secure random number generator.
 * 
 * @param lower the lower bound.
 * @param upper the upper bound.
 * @return the random integer.
 */
public int nextSecureInt(int lower, int upper) {
      if (lower >= upper) {
          throw new IllegalArgumentException
            ("lower bound must be < upper bound");
      }
      SecureRandom sec = getSecRan();
      return lower + (int) (sec.nextDouble() * (upper - lower + 1));
}