方法一:
String code = (Math.random()+"").substring(2,8)
效率较低。
方法二:
String code = String.valueOf((int)((Math.random()*9+1)*Math.pow(10,5)));
Math.pow(x,y):计算x的y次方。
Math.pow(10,5):100000
Math.random()*9:0-9(不包括9)
Math.random()*9+1:1-10 (不包括10)
Math.random()*9+1)*Math.pow(10,5):100000-1000000 (不包括1000000)
效率较高,因为数字的运算要比字符串操作要快。
错误方法:
String code = String.valueOf(new Random().nextInt(1000000));
Random().nextInt(1000000):随机生成的数在0-1000000之间,不包括1000000。