- 论坛徽章:
- 0
|
random使用练习
public class RandomTest {
public static void main(String[] args) {
for(int i=1; i=100; i++) {
System.out.print(randomInt(10,20) + "\t");
if(i%10==0) {
System.out.println();
}
}
}
public static double randomDouble() {
double x = Math.random();
return x;
}
public static double randomDouble(int high) {
double x = Math.random();
return x*high;
}
public static double randomDouble(int low, int high) {
if(low > high) {
System.out.println("Error");
return 0.0;
}
double x = Math.random();
return x * (high-low) + low ;
}
public static int randomInt(int low, int high) {
if(low > high) {
System.out.println("Error");
return 0;
}
double x = Math.random();
return (int)(x * (high-low) + low) ;
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/93948/showart_1877612.html |
|