Using Random in Java

How to use random in Java ?


This is my reference about random :

  1. Random number.
  2. Random generator combine alphabet and numeric.

Example 1 :

import java.util.random;

Random random = new Random();

//The range between 1000 and 9999.

int r = random.nextInt(8999) + 1000;

System.out.println(“Your number is “+r);

Example 2 :

//Use this library in java.

import java.util.random;

//Declaration the variabel (8 arrays) with data type : char.

char process[]= new char[8];

//I want to make a random with range(0..9,A..Z, and a..z).

final String combo = “1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”;

//This is for length of combo.

final int N = combo.length();

//This is declaration of random.

Random r = new Random();


for (int i = 0; i < 8; i++) {

process[i] = combo.charAt(r.nextInt(N));


//Convert char into String.

String result = String.valueOf(process);



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s