New file |
| | |
| | | package com.dy.common.util; |
| | | |
| | | import java.util.Random; |
| | | |
| | | @SuppressWarnings("unused") |
| | | public class CreateRandom { |
| | | |
| | | public static int random(int max, int min){ |
| | | return new Random().nextInt(max) % (max - min + 1) + min; |
| | | } |
| | | |
| | | /* |
| | | public static void main(String[] args) { |
| | | // System.out.println(CreateRandom.random(1, 0)); |
| | | // System.out.println(CreateRandom.random(2, 0)); |
| | | // System.out.println(CreateRandom.random(3, 0)); |
| | | // System.out.println(CreateRandom.random(4, 0)); |
| | | // System.out.println(CreateRandom.random(5, 0)); |
| | | // System.out.println(CreateRandom.random(100, 0)); |
| | | // System.out.println(CreateRandom.random(1256, 1234)); |
| | | System.out.println("======================="); |
| | | for(int i = 0 ; i < 100; i++){ |
| | | System.out.println(create_between(10, 100)); |
| | | } |
| | | } |
| | | */ |
| | | |
| | | |
| | | /** |
| | | * 4位随机数据 |
| | | * @return 随机数 |
| | | */ |
| | | public static int Random_4(){ |
| | | return new Random().nextInt(9999) % (9999 - 1000 + 1) + 1000; |
| | | } |
| | | /** |
| | | * 5位随机数据 |
| | | * @return 随机数 |
| | | */ |
| | | public static int Random_5(){ |
| | | return new Random().nextInt(99999) % (99999 - 10000 + 1) + 10000; |
| | | } |
| | | /** |
| | | * 6位随机数据 |
| | | * @return 随机数 |
| | | */ |
| | | public static int Random_6(){ |
| | | return new Random().nextInt(999999) % (999999 - 100000 + 1) + 100000; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 创建scape位随机数 |
| | | * @param scape 位数 |
| | | * @return 随机数 |
| | | */ |
| | | public String create(int scape){ |
| | | if(scape < 1){ |
| | | scape = 6 ; |
| | | } |
| | | double d = Math.random(); |
| | | String s = String.valueOf(d); |
| | | int index ; |
| | | String ss ; |
| | | try{ |
| | | index = s.indexOf('.') + 1; |
| | | ss = s.substring(index , index + scape); |
| | | } catch(Exception e){ |
| | | ss = "740414"; |
| | | } |
| | | return ss ; |
| | | } |
| | | |
| | | /** |
| | | * 创建两个整数之间的随机数 |
| | | * @param min 最小数 |
| | | * @param max 最大数 |
| | | * @return 随机数 |
| | | */ |
| | | public static int create_between(int min , int max){ |
| | | if(max < min){ |
| | | return min ; |
| | | } |
| | | if(max - min < min/2){ |
| | | return min ; |
| | | } |
| | | int len = String.valueOf(min).length() ; |
| | | String s = String.valueOf(Math.random() * 10000000); |
| | | s = s.substring(0 ,len) ; |
| | | int n = Integer.parseInt(s) ; |
| | | if(n < min || n > max){ |
| | | n = create_between(min , max) ; |
| | | } |
| | | return n ; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 得到一个小于max的随机数 |
| | | * @param max 最大数 |
| | | * @return 随机数 |
| | | */ |
| | | public int create_less(int max){ |
| | | if(max > 9){ |
| | | max = 9 ; |
| | | } |
| | | double d = Math.random(); |
| | | int n = 0; |
| | | int m; |
| | | String s = String.valueOf(d); |
| | | for(int i = 4 ; i < s.length() ; i++){ |
| | | m = Integer.parseInt(s.charAt(i)+""); |
| | | if(m < max){ |
| | | n = m ; |
| | | break ; |
| | | } |
| | | } |
| | | return n ; |
| | | } |
| | | |
| | | } |