mercredi 9 septembre 2020

Java Random vs SecureRandom for non-security and general random number generation purposes

I have the following two code implementations for random number generation in Java.

new Random()

// create instance of Random class 
Random rand = new Random(); 
// Generate random integers in range 0 to 999 
int rand_int1 = rand.nextInt(1000); 

new SecureRandom()

// create instance of SecureRandom class 
SecureRandom rand = new SecureRandom();   
// Generate random integers in range 0 to 999 
int rand_int1 = rand.nextInt(1000);

I know that a Random class has only 48 bits whereas SecureRandom can have up to 128 bits. So the chances of repeating in SecureRandom are smaller. SecureRandom is also recommended for security-related applications. (See this article)

Question: I have a simple (non-security-related) application that needs a random number generated. Would someone still recommend using new SecureRandom() over new Random() for this purpose? (What would be more performant for this purpose?)




Aucun commentaire:

Enregistrer un commentaire