• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Generating random numbers in java

May 25, 2011 By j2eereference Leave a Comment

Java provides classes to help you for generating random numbers. There are two ways you can generate random numbers using java.

The below examples show you how to create a random number and also help you to understand and use the code to generate random numbers in your application.

1.      Random number in a specified range

Here you can specify the range within you want to generate the random numbers. Below code will explain you how to generate random numbers

import java.util.*;

 

public class RandomNumbers{

  public static void main(String[] args){

Random random = new Random();

int randomNumber = random.nextInt(5);

System.out.println(“Random Number between 0 to 5 : ” + randomNumber);

}

}

Output:

Random Number between 0 to 5 : 1

 

2.      Random number without specifying the range

 import java.util.*;

 

public class RandomNumbers{

public static void main(String[] args){

Random random = new Random();

int randomNumber = random.nextInt();

System.out.println(“Random Number generated : ” + randomNumber);

}

}

Output:

Random Number generated : -866975367

Related Posts

  • Busy Spinning in mutithreading
  • Implementing queue in java
  • TreeSet vs ConcurrentSkipListSet
  • How to create immutable class?
  • Implementing Stack in java
  • What is ReentrantLock?
  • What is Semaphore in java
  • Why AtomicInteger class
  • What is CyclicBarrier?
  • CountDownLatch in java

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.