首页 > 其他 > 详细

Random类与Random方法

时间:2019-04-09 18:40:29      阅读:149      评论:0      收藏:0      [点我收藏+]
 class Test{
    public static void main(String[] args){

        int min=2;    //定义随机数的最小值
        int max=102;    //定义随机数的最大值
        //产生一个2~100的数
        int s=(int) min+(int)(Math.random()*(max-min));
        if(s%2==0)    //如果是偶数就输出
            System.out.println("随机数是:"+s);
        else    //如果是奇数就加1后输出
            System.out.println("随机数是:"+(s+1));
    }
}

技术分享图片

import java.util.Random;
public class April_9 {

    public static void main(String[] args) {
        Random suiji=new Random();
        double d1=suiji.nextDouble();
        int d2=suiji.nextInt(1) -8;
        //随机数生成时,bound括号外面值默认为0,但是如果设置了别的数,
        // 那么bound括号内的数减去外部的值作为右半边开区间,所以上述范围在[-8,-7)
       Boolean d3=suiji.nextBoolean();
        System.out.println(d1+"\n"+d2+"\n"+d3);
        System.out.println("生成9位开奖彩票号码为:");
        for (int i=1;i<10;i++){
            int d4=suiji.nextInt(9);
            System.out.print(d4);
        }

    }
}

技术分享图片

 

Random类与Random方法

原文:https://www.cnblogs.com/hetaoyuan/p/10678686.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!