Skip to content

Instantly share code, notes, and snippets.

@thirupathi-chintu
Created July 3, 2018 15:15
Show Gist options
  • Save thirupathi-chintu/d64a92f521553e27bf55c526e73c7caa to your computer and use it in GitHub Desktop.
Save thirupathi-chintu/d64a92f521553e27bf55c526e73c7caa to your computer and use it in GitHub Desktop.
OTP Creater
// 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