使用java.lang.Math 生成随机数字
2013-02-28 22:17
615 查看
这里我想说生成随机数字,并且使用 java.lang.Math 这个类。内容很少,直接上代码吧。
RamdomUtil
打印:
0.16326098275998668
77.95240649484046
04574445034710284
197328824864798
------------------------------
1362061365776
136206136577600
---------业务api---------------------
5位随机数----98543
5位随机数----97315
5位随机数----00175
5位随机数----82552
5位随机数----76951
5位随机数----29536
5位随机数----02657
5位随机数----11367
5位随机数----93038
5位随机数----10604
---------业务api--------end-------------
RamdomUtil
package org.xml.util; public class RamdomUtil { /** * 使用Math.random()生成随机小数0-1之间 * @return 0.2828253436134537 */ public static String createRandomDoubleWithDotByMath(){ String res=""; Double d=Math.random(); res=String.valueOf(d); return res; } /** * 使用Math.random()生成随机小数0-1之间,再乘以参数multiplier * @return 23.00129411460935 */ public static String createRandomDoubleWithDotByMath(int multiplier){ String res=""; if(multiplier<=0){ multiplier=1; } Double d=Math.random()*multiplier; res=String.valueOf(d); return res; } /** * 使用Math.random()生成随机小数0-1之间,只要小数点后面的数字 * @return 2314994860279005 */ public static String createRandomDoubleWithoutDotByMath(){ String res=""; Double d=Math.random(); res=String.valueOf(d); res=res.substring(res.indexOf(".")+1); return res; } /** * 使用Math.random()生成随机小数0-1之间,再乘以参数multiplier,只要小数点后面的数字 * @return 911269130333089 */ public static String createRandomDoubleWithoutDotByMath(int multiplier){ String res=""; if(multiplier<=0){ multiplier=1; } Double d=Math.random()*multiplier; res=String.valueOf(d); res=res.substring(res.indexOf(".")+1); return res; } /** * 系统当前时间的毫秒数 * @return 1361773407554 */ public static String createRandomNumberBySystemTime(){ String res=""; Long ltime=System.currentTimeMillis(); res=String.valueOf(ltime); return res; } /** * 系统当前时间的毫秒数,再乘以multiplier * @return 136177358759900 */ public static String createRandomNumberBySystemTime(int multiplier){ String res=""; if(multiplier<=0){ multiplier=1; } Long ltime=System.currentTimeMillis()*multiplier; res=String.valueOf(ltime); return res; } public static void main(String[] ss){ String r1=RamdomUtil.createRandomDoubleWithDotByMath(); System.out.println(r1); String r2=RamdomUtil.createRandomDoubleWithDotByMath(100); System.out.println(r2); String r3=RamdomUtil.createRandomDoubleWithoutDotByMath(); System.out.println(r3); String r4=RamdomUtil.createRandomDoubleWithoutDotByMath(100); System.out.println(r4); System.out.println("------------------------------"); String r5=RamdomUtil.createRandomNumberBySystemTime(); System.out.println(r5); String r6=RamdomUtil.createRandomNumberBySystemTime(100); System.out.println(r6); System.out.println("---------业务api---------------------"); for(int a=0;a<10;a++){ String r7=RamdomUtil.get5RandomNumber(); System.out.println("5位随机数----"+r7); } System.out.println("---------业务api--------end-------------"); } ///////////以下是供业务上使用的api////////////////////////////////////////////////////////////////// //生成5位随机数字 public static String get5RandomNumber(){ String res=""; String one=RamdomUtil.createRandomDoubleWithoutDotByMath(); if(one.length()<5){ one+=RamdomUtil.createRandomDoubleWithoutDotByMath(2); } res=one.substring(one.length()-5, one.length()); return res; } }
打印:
0.16326098275998668
77.95240649484046
04574445034710284
197328824864798
------------------------------
1362061365776
136206136577600
---------业务api---------------------
5位随机数----98543
5位随机数----97315
5位随机数----00175
5位随机数----82552
5位随机数----76951
5位随机数----29536
5位随机数----02657
5位随机数----11367
5位随机数----93038
5位随机数----10604
---------业务api--------end-------------
相关文章推荐
- 使用java Random动态传递位数 生成指定位数的随机字符串-数字字母混合
- 使用js Math.random()函数生成n到m间的随机数字
- 使用js Math.random()函数生成n到m间的随机数字
- java 使用UUID生成19随机单号 纯数字,数字+字母唯一不重复单号
- JS实现使用Math.random()函数生成n到m间的随机数字
- java 一个int数组 长度为100 随机生成100个数 即1-100 将其插入进数组 插入的数字不能重复
- Java随机生成6位数字
- Java随机生成6位数字
- java随机生成一个6/8/12位密码,数字加字母的密码
- Java随机生成6位数字
- java随机生成邀请码(数字+字母)
- Java生成随机密码(包含大小写字母和数字)
- 使用JXL生成Excel时发生java.lang.ArrayIndexOutOfBoundsException错误
- JAVA随机数生成 | Math.random()方法 | 随机生成int、double类型
- 【Java】利用Math.Random()方法随机出四个字符的字符串,字符包括数字,大小写英文字母
- JAVA生成n位随机字符串,数字+英文字母+标点符号
- java生成随机数字和字母组合
- Java随机生成长度不少于6的指定长度的密码,且这个字符串必须包含大小写字母、数字和特殊字符,四种的任意三种
- Java随机生成6位数字
- java生成随机数字和字母组合