🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the SecureRandom nextInt() method in Java programming and how to use it with an example.
1. SecureRandom nextInt() Method Overview
Definition:
The nextInt(int bound) method is used to generate a cryptographically strong random integer between 0 (inclusive) and the specified bound (exclusive).
Syntax:
public int nextInt(int bound)
Parameters:
- bound: The upper bound (exclusive) on the random number to be returned. Must be positive.
Key Points:
- The nextInt(int bound) method is used to generate a cryptographically strong random integer between 0 (inclusive) and the specified bound (exclusive).
- Throws IllegalArgumentException if the bound is not positive.
- SecureRandom is preferable over the Random class when higher quality randomness is required, especially in security-sensitive applications.
- The SecureRandom instance can be seeded using a user-provided seed or automatically by the system.
- This method may block as entropy is being gathered, especially if it is invoked shortly after the instance is created.
2. SecureRandom nextInt() Method Example
import java.security.SecureRandom;
public class SecureRandomExample {
public static void main(String[] args) {
// Create a SecureRandom object
SecureRandom secureRandom = new SecureRandom();
// Generate random integers within a given bound
int randomInt1 = secureRandom.nextInt(100); // bound is 100
int randomInt2 = secureRandom.nextInt(1000); // bound is 1000
// Print the generated random integers
System.out.println("Random Integer within 0 to 100: " + randomInt1);
System.out.println("Random Integer within 0 to 1000: " + randomInt2);
}
}
Output:
Random Integer within 0 to 100: 63 Random Integer within 0 to 1000: 827 (Note: The actual output may vary each time the program is executed as the numbers are randomly generated.)
Explanation:
In this example, a SecureRandom object is created, and two random integers within different bounds are generated using the nextInt(int bound) method. The generated random integers are then printed to the console, demonstrating the method's ability to produce cryptographically strong random integers within a specified range.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment