Java - Random and Secure Random with Example - Generate random number in Java

Random

java.util
Class Random

public class Random extends Object implements Serializable



The classes defined in Random are not cryptographically vigorous, and the numbers culled are not consummately random because a definite mathematical algorithm is utilized to cull them. Ergo, it is not safe to utilize this class for tasks that require a high caliber of security, like engendering an arbitrary password, etc.

The class utilizes a 48-bit seed

Instances of java.util.Random are threadsafe. However, the concurrent utilization of the same java.util.Random instance across threads may encounter contention and consequent poor performance. Consider instead utilizing ThreadLocalRandom in multithreaded designs.

Instances of java.util.Random are not cryptographically secure. Consider instead utilizing SecureRandom to get a cryptographically secure pseudo-desultory number engenderer for use by security-sensitive applications.

Java Random Example:
package com.knowledgefactory;

import java.util.Random;

/*
* A Java program to demonstrate random number
generation using java.util.Random;
import java.util.Random;
* */

public class KnowledgeFactoryRandom {

public static void main(String[] argv) {
// create instance of Random class
Random rand = new Random();
// Generate random integers in range 0 to 9999
int value = rand.nextInt(10000);
// Print random integers
System.out.println("Random Integers:" + value);

}
}

SecureRandom


java.security

Class SecureRandom

public class SecureRandom extends Random

This class provides a cryptographically random arbitrary number engenderer (RNG).
A cryptographically vigorous random number minimally complies with the statistical desultory number engenderer tests designated in FIPS 140-2, Security Requisites for Cryptographic Modules, section 4.9.1. Supplementally, SecureRandom must engender non-deterministic output. Consequently any seed material passed to a SecureRandom object must be capricious, and all SecureRandom output sequences must be cryptographically vigorous.
A caller obtains a SecureRandom instance via the no-argument constructor or one of the getInstance methods:

SecureRandom arbitrary = incipient SecureRandom();


Java SecureRandom Example:
package com.knowledgefactory;

import java.security.SecureRandom;

/*
* A Java program to demonstrate random number
* generation using java.security.SecureRandom ;
* import java.security.SecureRandom ;
* */

/**
* @author www.knowledgefactory.net
*/

public class KnowledgeFactorySecureRandom {

public static void main(String[] argv) {
// create instance of SecureRandom class
SecureRandom rand = new SecureRandom();
// Generate random integers in range 0 to 9999
int value = rand.nextInt(10000);
// Print random integers
System.out.println("Random Integers:" + value);

}
}


More...

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete