java生成随机数

一、

1、生成0-2之间的随机数,包括2

Random rand = new Random();

int randNum = rand.nextInt(3);

2、生成5-26之间的随机数,包括26

int randNum = rand.nextInt(22)+5;

 

3、java中让随机数产生在一个区间段:

假设你要产生5到10之间的随机数,可以用下面方法:

int Min = 5;

int Max = 10;

int result = Min + (int)(Math.random() * ((Max - Min) + 1));

 

4、生成六位的随机数:

int rd = (int)((Math.random()*9+1)*100000);

 

5、Math.random()方法是一个可以产生[0.0,1.0]区间内的一个双精度浮点数的方法
如:
产生一个100以内的整数:int x=(int)(Math.random()*100);
又如:
产生一个1-50之间的随机数:int x=1+(int)(Math.random()*50)

6、

/**
     * 获取count个随机数
     * @param count 随机数个数
     * @return
     */
    public static String game(int count){
        StringBuffer sb = new StringBuffer();
        String str = "0123456789";
        Random r = new Random();
        for(int i=0;i<count;i++){
            int num = r.nextInt(str.length());
            sb.append(str.charAt(num));
            str = str.replace((str.charAt(num)+""), "");
        }
        return sb.toString();
    }
 
    public static void main(String[] args) {

        System.out.println(game(4));
    }

7、java 随机生成11位 组合


public static String generate8RateUuid() {  
        String[] chars = new String[] { "a", "b", "c", "d", "e", "f",  
                "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",  
                "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",  
                "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",  
                "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",  
                "W", "X", "Y", "Z" };  
        Random r = new Random();
        StringBuffer shortBuffer = new StringBuffer();  
        String uuid = UUID.randomUUID().toString().replace("-", "");  
        for (int i = 0; i < 8; i++) {  
            String str = uuid.substring(i * 4, i * 4 + 4);  
            int x = Integer.parseInt(str, 16);  
            shortBuffer.append(chars[x % 0x3E]);  
        }  
        return shortBuffer.toString()+(r.nextInt(899)+100);  
    }

二、

1、去掉指定的子字符串:

String filename =worker2.getWorkerScanFront();///upload/workerCard/1542200260139105670.jpeg
String removeStr = "/upload/workerCard";
filename = filename.replace(removeStr, "");

 

转载于:https://my.oschina.net/8824/blog/2876512

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值