当前位置:网站首页>How to generate six digit verification code

How to generate six digit verification code

2022-07-06 16:49:00 Daily enlightenment

Method 1 :

String code = (Math.random()+"").substring(2,8)

Low efficiency .

Method 2 :

String code = String.valueOf((int)((Math.random()*9+1)*Math.pow(10,5)));

Math.pow(x,y): Calculation x Of y Power .

Math.pow(10,5):100000

Math.random()*9:0-9( barring 9)

Math.random()*9+1:1-10 ( barring 10)

Math.random()*9+1)*Math.pow(10,5):100000-1000000 ( barring 1000000)

More efficient , Because the number operation is faster than the string operation .

Wrong way

String code = String.valueOf(new Random().nextInt(1000000));

Random().nextInt(1000000): The randomly generated number is in 0-1000000 Between , barring 1000000.

原网站

版权声明
本文为[Daily enlightenment]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131314159713.html