Random类

一、Random
1、创建Random类型的对象:

Random random = new Random();//默认构造方法
Random random = new Random(10010010);//指定种子数字

2、生成随机数字:

生成随机整数:
int k = random.nextInt();

生成随机长整数:
long l = random.nextLong();

3、生成指定范围的数字:

例如生成0-10之间的随机数字:
int k = random.nextInt();
int j = Math.abs(k % 10);

//直接方式:rand.nextInt(range);
int i =random.nextInt(10);

获得10-20之间的随机数字:
int k = random.nextInt();
int j = Math.abs(k % 10) + 10;

int i = (int)(Math.random()*1000)

random对象的nextInt(),nextInt(int n)说明:

int nextInt()
  返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。 有正有负。
int nextInt(int n)
  返回一个伪随机数,它是从此随机数生成器的序列中取出的、在 0(包括)和指定值(不包括)之间均匀分布的 int值。

4、next(int bits)

next(n)产生2的n次方之内的随机数,next(32)产生2的32次方之内的随机数,就相当于nextInt()了。

5、setSeed(long seed)

setSeed(long seed) 用于设定随机数的种子,即这里的seed。随机数种子的用处是:一般说来,这里的Random类产生随机数是伪随机数,是系统采用特定的算法生成出来的,方法是new两个Random类random1和random2。各自调用nextInt方法10次,我们可以看出,虽然各自产生的是随机数,但是两个Random类产生的随机数都是一样的。这就使得随机数存在着漏洞。

二、java disabuse

Java代码
/***********双括弧初始化:内层的花括号就是匿名类的初始化子句************/
@SuppressWarnings("unchecked")
private static Map map = new HashMap() {{
put("id", "20090501");
put("name", "name1");
put("age","20");
}};

private static final Set<String> set = new HashSet<String>() {{
add("lucy");
add("lily");
add("frank");
add("bruce");
}};

public static void printSet(Set set){
Iterator it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}

public static void printMap(Map map) {
for (Object key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
}

public static void printSelf() {
// 正则表达式“.”可以匹配任何单个的字符,要转义
System.out.println(JavaDisabuse.class.getName().replaceAll("\\.", "/")
+ ".class");
// java.util.regex.Pattern.quote。它接受一个字符串作为参数,并可以添加必
// 需的转义字符,它将返回一个正则表达式字符串
System.out.println(JavaDisabuse.class.getName().replaceAll(
Pattern.quote("."), "/")+ ".class");

//java.util.regex.Matcher.quoteReplacement,它将字
//符串转换成相应的替代字符串。
System.out.println(JavaDisabuse.class.getName().replaceAll("\\.",
Matcher.quoteReplacement(File.separator)) + ".class");

System.out.println(JavaDisabuse.class.getName().
replace('.', File.separatorChar) + ".class");
}

/** ***********提防溢出*************** */
public static void longDivision() {
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;// 乘完后(已溢出)转型
final long MICROS_PER_DAY_L = 24L * 60 * 60 * 1000 * 1000;
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;

System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);// 5
System.out.println(MICROS_PER_DAY_L / MILLIS_PER_DAY);// 1000
}

/** ***********执行精确小数运算********* */
public static BigDecimal subtract(String arg0, String arg1) {
// 问题在于1.1 这个数字不能被精确表示成为一个double,因此它被表示成为最
// 接近它的double值。
System.out.println(2.00 - 1.10);// 二进制浮点数的double运算:0.8999999999999999
return new BigDecimal(arg0).subtract(new BigDecimal(arg1));// 0.90
}

/** *******求奇数(莫用i%2==1,负数失效)********* */
public static boolean isOdd(int i) {
return i % 2 != 0;
}

public static boolean isOdd1(int i) {
return (i & 1) != 0;
}

posted @ 2012-02-26 12:54  rason2008  阅读(1330)  评论(0编辑  收藏  举报