Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

how to use Random class using collections in java

how to use Random class using collections in java

Previous Home Next

 

In this example we will show you how to use the Random class.

For using the Random class first of all  we have  import the java.util package in which the java.util.Random class is contained.  We have created a class named CollectionExample in which we have created an object of the Random class and performed various operations.
 In the following example we have created an object of the Random class using its default constructor as,

Random
rndm=new Random();
This method creates a new random number generator.
We have also used the nextInt() method of the Random which is having the following syntax,
int nextInt(int val)
The above method returns a pseudorandom, uniformly distributed int value between 0  and specified value in the method, drawn from the current random number generator's sequence.

In this example we have created a class named CollectionExample inside which we have created a Random class object named rndm. Then we have taken an integer type variable named number to hold the random values generated by the method nextInt(int val) on the object of the Random class i.e rndm(in this example), then we have displayed some strings using if-else condition. 

 
package r4r.co.in;
import java.util.*;


public class CollectionExample
{

/**
* @param args
*/

public static void main(String args[]) throws IllegalArgumentException
{
try
{
Random rndm=new Random(); // creates an object of Random class
int numbers; // created a variable named numbers of integer type
numbers=rndm.nextInt(4)+1; // assigning the random numbers generated between 0-4 to numbers
System.out.println(numbers); // displaying the number generated
if(numbers==4) // if number generated equals to 4 then
{
System.out.println("hi!!!!!!!"); // print hi!!!!!!
}
else
{
System.out.println("bye!!!!!!!"); // otherwise print bye!!!!!!!
}

}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}


}
}



The above example gives the following output:

2
bye!!!!!!!

4
hi!!!!!!!

Previous Home Next