How to generate OTP Number with 6 digits

2020-02-26 02:08发布

What is an OTP number in a login authentication system? Is there any specific algorithm for generating OTP numbers using java (android). Or is an OTP something like random number? How can this be achieved, with optimization.

8条回答
Anthone
2楼-- · 2020-02-26 02:52

Easiest way is to just use DecimalFormat with Random class.

String otp= new DecimalFormat("000000").format(new Random().nextInt(999999));
System.out.println(otp);

Sample Outputs,

002428
445307
409185
989828
794486
213934
查看更多
狗以群分
3楼-- · 2020-02-26 02:52
public static void main(String []args){
            java.util.Random r=new java.util.Random();
            int otp = r.nextInt(1000000); // no. of zeros depends on the OTP digit
            System.out.println(otp);
}
查看更多
登录 后发表回答