Random Code Generator || Java

Janaka Chinthana Dissanayake
2 min readJul 16, 2021

You can easily generate random code as OTP or something that you needed, using the below code segment. import statement is much easier such as

Eg: -

import java.util.Random;

class GenerateRandom {

public static void main( String args[] ) {

Random rand3 = new Random(); //instance of random class

int upperbound = 25;

//generate random values from 0–24

int int_random = rand.nextInt(upperbound);

System.out.println(int_random);

}

}

In the above code, it will be generated a single or double number. if you want more numbers as a single code you can add multiple instances like the below code segment. It will be more secure than the above code.

import java.util.Random;

class GenerateRandom {

public static void main( String args[] ) {

Random rand = new Random();

Random rand1 = new Random();

Random rand2 = new Random();

Random rand3 = new Random(); //instance of random class

int upperbound = 25;

//generate random values from 0–24

int int_random = rand.nextInt(upperbound);

int int_random1 = rand1.nextInt(upperbound);

int int_random2 = rand2.nextInt(upperbound);

int int_random3 = rand3.nextInt(upperbound);

System.out.println( int_random+””+int_random1+””+int_random2+””+int_random3);

}

}

--

--

Janaka Chinthana Dissanayake
0 Followers

I am an undergraduate student of SLIIT following BSc.(HonS ) in IT specialization in Software Engineering