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

这里我想说生成随机数字,并且使用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-------------







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值