I am trying to use a SecureRandom
to generate random numbers in my java project. But I am a little confused as how to keep my object for SecureRandom
. Should it be a static
class member. I dont intend to call this from outside. Below is my current implementation :
Class MyClass {
private static final SecureRandom secureRandom = new SecureRandom();
private long calculate(int noOfRetry){
final long value = someValueCalculationWith-noOfRetry;
final float randomNo = secureRandom().nextFloat() + 1;
return (long) (value*randomNo);
}
}
Is this the correct way to use SecureRandom in java ?
float
anddouble
have very interesting properties w.r.t. precision, rounding and randomness. When I see afloat
ordouble
being used in a security application I always feel a little shudder - something may be out of kilt. Use e.g.nextInt(int max)
instead. – Spoon