`
lxr215
  • 浏览: 58997 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

random使用练习

    博客分类:
  • Java
 
阅读更多

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) ;
    }
    
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics