您的位置:首页 > 编程语言 > Java开发

使用java.lang.Math 生成随机数字

2013-02-28 22:17 615 查看
这里我想说生成随机数字,并且使用 java.lang.Math 这个类。内容很少,直接上代码吧。

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-------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: