生成随机数 随机生成字符创

package test;


import java.util.Random;


public class CountFigure {


public static void main(String[] args) {

getRandomInt();
 getRandomString(30);


}
public static void getRandomInt() {
/**
* Math.random()方法是一个可以产生[0.0,1.0]区间内的一个双精度浮点数的方法
* 产生一个100以内的整数:int x=(int)(Math.random()*100);
* 产生一个1-50之间的随机数:int y=1+(int)(Math.random()*50)
*/
Random random = new Random();
int x=(int)(Math.random()*100);
int y=1+(int)(Math.random()*50);
System.out.println("随机生成100以内的整数是:"+ random.nextInt(x));
   System.out.println("随机生成1-50之间的整数是:"+random.nextInt(y));


}

public static String getRandomString(int length) { //length表示生成字符串的长度  
/**
* Math.random()方法是一个可以产生[0.0,1.0]区间内的一个双精度浮点数的方法
* 随机生成字符串:int x=(int)(Math.random()*100);

*/
   String base = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";     
   Random random = new Random();     
   StringBuffer sb = new StringBuffer();     
   for (int i = 0; i < length; i++) {     
       int number = random.nextInt(base.length());     
       sb.append(base.charAt(number));     
   }   
   System.out.println(sb.toString());
   return sb.toString();  
   
}     




}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值