随机生成位数工具类 RandomUtils(包含字母的)

import java.util.Random;

/**
 * 随机生成位数工具类
 * @author xc
 */
public class RandomUtils {

//	public static void main(String[] args) {
//		Random random = new Random();
//		System.out.println(random.nextInt(4));
//		
//	}
	
	/**
	 * 随机数50-150
	 */
	public static int getRandomNum(){
		Random random = new Random();
		int x = random.nextInt(100);
		return x+50;
	}
	
	/**
	 * 手机注册六位校验码生成
	 */
	public static String getRandomNum6(){
		Random random = new Random();
		int x = random.nextInt(899999);
		return x+100000+"";
	}
	
	/**
	 * 手机注册四位校验码生成
	 */
	public static String getRandomNum4(){
		Random random = new Random();
		int x = random.nextInt(8999);
		return x+1000+"";
	}
	
	/**
	 * 生产3位数的随机数
	 */
	public static String getRandomNum3(){
		Random random = new Random();
		int x = random.nextInt(899);
		return x+100+"";
	}
	
	/**
	 * 生产20至50的随机数
	 */
	public static int getRandomNumTwentyToFifty  (){
		Random random = new Random();
		int x = random.nextInt(30);
		return x+20;
	}

  /**
  * 生成n个包含数字和字母的字符串
  */
	 public static String getItemID( int n ) {
        String val = "";
        Random random = new Random();
        for ( int i = 0; i < n; i++ )
        {
            String str = random.nextInt( 2 ) % 2 == 0 ? "num" : "char";
            if ( "char".equalsIgnoreCase( str ) )
            { // 产生字母
                int nextInt = random.nextInt( 2 ) % 2 == 0 ? 65 : 97;
                // System.out.println(nextInt + "!!!!"); 1,0,1,1,1,0,0
                val += (char) ( nextInt + random.nextInt( 26 ) );
            }
            else if ( "num".equalsIgnoreCase( str ) )
            { // 产生数字
                val += String.valueOf( random.nextInt( 10 ) );
            }
        }
        return val;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LC超人在良家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值