Created
July 3, 2018 15:15
-
-
Save thirupathi-chintu/d64a92f521553e27bf55c526e73c7caa to your computer and use it in GitHub Desktop.
OTP Creater
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java code to explain how to generate OTP | |
// Here we are using random() method of util | |
// class in Java | |
import java.util.*; | |
public class otp | |
{ | |
static char[] OTP(int len) | |
{ | |
System.out.println("Generating OTP using random() : "); | |
System.out.print("You OTP is : "); | |
// Using numeric values | |
String numbers = "0123456789"; | |
// Using random method | |
Random rndm_method = new Random(); | |
char[] otp = new char[len]; | |
for (int i = 0; i < len; i++) | |
{ | |
// Use of charAt() method : to get character value | |
// Use of nextInt() as it is scanning the value as int | |
otp[i] = | |
numbers.charAt(rndm_method.nextInt(numbers.length())); | |
} | |
return otp; | |
} | |
public static void main(String[] args) | |
{ | |
int length = 4; | |
System.out.println(OTP(length)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment