java生成N位随机数字

直接上代码

/**
 * 生成N位随机数字
 */
public static Integer randomNumber(int length){
    return (new Double(Math.random() * (Math.pow(10F, length) - Math.pow(10F, length - 1)) + Math.pow(10F, length - 1))).intValue();
}

此方式可保证例如length=5时不会出现5位数以下的数字,适合用于短信验证码等固定位数的随机数,在单线程测试下一秒可生成超过一亿个随机数。

猜你喜欢

转载自blog.csdn.net/u011580177/article/details/103102851