package com.dy.common.util;
|
|
import java.util.Random;
|
|
public class CreateRadom {
|
|
public static int radom(int max, int min){
|
return new Random().nextInt(max) % (max - min + 1) + min;
|
}
|
|
public static void main(String[] args) {
|
System.out.println(CreateRadom.radom(1, 0));
|
System.out.println(CreateRadom.radom(2, 0));
|
System.out.println(CreateRadom.radom(3, 0));
|
System.out.println(CreateRadom.radom(4, 0));
|
System.out.println(CreateRadom.radom(5, 0));
|
System.out.println(CreateRadom.radom(100, 0));
|
System.out.println(CreateRadom.radom(1256, 1234));
|
}
|
|
/**
|
* 4位随机数据
|
* @return 随时数
|
*/
|
public static int radom_4(){
|
return new Random().nextInt(9999) % (9999 - 1000 + 1) + 1000;
|
}
|
/**
|
* 5位随机数据
|
* @return 随时数
|
*/
|
public static int radom_5(){
|
return new Random().nextInt(99999) % (99999 - 10000 + 1) + 10000;
|
}
|
/**
|
* 6位随机数据
|
* @return 随时数
|
*/
|
public static int radom_6(){
|
return new Random().nextInt(999999) % (999999 - 100000 + 1) + 100000;
|
}
|
|
|
/**
|
* 创建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 ;
|
}
|
String mins = String.valueOf(min) ;
|
int len = mins.length() ;
|
char minfirst = mins.charAt(0) ;
|
double d = Math.random();
|
d = d * 10000000 ;
|
String s = String.valueOf(d);
|
s = minfirst + s ;
|
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 int 最大值
|
* @return int 随时数
|
*/
|
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 ;
|
}
|
|
}
|