JAVA Random 随机类
nextInt 方法 得到一个随机整数, 可以指定范围
package object; import static net.util.Print.*; import java.util.Random; public class Test{ public static void main(String[] args){ Random rand = new Random(); for(int i=0;i<100;i++){ int j = rand.nextInt(100)+1;//调用Rand 方法 printnb(j+"\t"); } } }
/*******************here is Rand nextInt display **************************
public int nextInt(int bound) {
if (bound <= 0)
throw new IllegalArgumentException(BadBound);
int r = next(31);
int m = bound - 1;
if ((bound & m) == 0) // i.e., bound is a power of 2
r = (int)((bound * (long)r) >> 31);
else {
for (int u = r;
u - (r = u % bound) + m < 0;
u = next(31))
;
}
return r;
}*///~
nextFloat 方法 得到一个随机浮点数,不能指定范围
package object; import static net.util.Print.*; import java.util.Random; public class Test{ public static void main(String[] args){ Random rand = new Random(); for(int i=0;i<100;i++){ float j = rand.nextFloat(); printnb(j+"\t"); } } } /*****************this is nextFloat show*********************** * public float nextFloat() { * return next(24) / ((float)(1 << 24)); * } *///~