JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Program: Basic random number generator.


Description:

Many times we need to generate sequence of numbers. It is not a problem to generate sequence of numbers. But some times we need to generate random numbers. In java we can generate random numbers by using Random class. By using Random class, we can generate random integers, long numbers and double values. Below example shows how to generate random numbers using Random class.


Code:
package com.java2novice.random;

import java.util.Random;

public class MyBasicRandom {

	public static void main(String a[]){
		Random rand = new Random();
		System.out.println("Random Integers:");
		System.out.println(rand.nextInt());
		System.out.println(rand.nextInt());
		System.out.println(rand.nextInt());
		System.out.println("Random Double Numbers:");
		System.out.println(rand.nextDouble());
		System.out.println(rand.nextDouble());
		System.out.println(rand.nextDouble());
		System.out.println("Random Long Numbers:");
		System.out.println(rand.nextLong());
		System.out.println(rand.nextLong());
		System.out.println(rand.nextLong());
	}
}

Output:
Random Integers:
1400247241
953993278
830962592
Random Double Numbers:
0.19841760470907022
0.32964328277263866
0.4624594748136943
Random Long Numbers:
4925512194471935414
2115815340178756456
4896741386551752249
Next Program >>

List of Random class sample programs:

  1. Basic random number generator.
  2. How to generate random numbers in the given range?
  3. How to generate same random sequence everytime?
  4. How to change Random class seed value?
  5. How to create random string with random characters?
Knowledge Centre
Procedural Vs Object-oriented Programs
In procedural program, programming logic follows certain procedures and the instructions are executed one after another. In OOP program, unit of program is object, which is nothing but combination of data and code.

In procedural program, data is exposed to the whole program whereas in OOPs program, it is accessible with in the object and which in turn assures the security of the code.
Famous Quotations
It is amazing what you can accomplish if you do not care who gets the credit.
-- Harry Truman

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.