• Java基础知识强化80:Math类random()方法的小扩展(控制产生目的数字的概率)


    1. Math.random()方法:

    Math.random()出现的数据是0.0<= x < 1.0之间,随机出现一个数据Math.random()>0.1概率是0.9

    那么如下:

    Math.random()>0.1? 2:4 

    上面表示出现数字2的概率为0.9,出现数字4的概率为0.1

    2. 上面出现的是两个数,如果我们案例需求是多个数,控制概率出现,该怎么办呢?

    下面是一个案例:

    做一个翻宝程序,通过返回数字0-5来判断中奖情况,

    012345,这几个数字的出现的概率是0出现最高,1出现比0少,2出现比1少,依次下去……

    实现代码:

    package com.himi.math;
    
    /**
     * JAVA 返回随机数,并根据概率、比率
     * 
     * @author hebao
     *
     */
    public class MathRandom {
        /**
         * 0 出现的概率为%50
         */
        public static double rate0 = 0.50;
        /**
         * 1 出现的概率为%20
         */
        public static double rate1 = 0.20;
        /**
         * 2 出现的概率为%15
         */
        public static double rate2 = 0.15;
        /**
         * 3 出现的概率为%10
         */
        public static double rate3 = 0.10;
        /**
         * 4 出现的概率为%4
         */
        public static double rate4 = 0.04;
        /**
         * 5 出现的概率为%1
         */
        public static double rate5 = 0.01;
    
        /**
         * Math.random()产生一个double型的随机数.
         * 判断一: 例如0出现的概率为%50,则介于0到0.50中间的返回0
         * 
         * @return int
         *
         */
        private int PercentageRandom() {
            double randomNumber;
            randomNumber = Math.random();// randomNumber = [0,1)
            if (randomNumber >= 0 && randomNumber <= rate0) {
                return 0;
    
            } else if (randomNumber >= rate0 / 100 && randomNumber <= rate0 + rate1) {
                return 1;
    
            } else if (randomNumber >= rate0 + rate1 && randomNumber <= rate0 + rate1 + rate2) {
                return 2;
    
            } else if (randomNumber >= rate0 + rate1 + rate2 && randomNumber <= rate0 + rate1 + rate2 + rate3) {
                return 3;
    
            } else if (randomNumber >= rate0 + rate1 + rate2 + rate3
                    && randomNumber <= rate0 + rate1 + rate2 + rate3 + rate4) {
                return 4;
    
            } else if (randomNumber >= rate0 + rate1 + rate2 + rate3 + rate4
                    && randomNumber <= rate0 + rate1 + rate2 + rate3 + rate4 + rate5) {
                return 5;
    
            }
            return -1;
        }
    
        /**
         * 测试主程序:
         * 打印出100000个随机数据,验证出现概率
         * 
         * @param agrs
         */
        public static void main(String[] agrs) {
            int i = 0;
            double count_0 = 0;
            double count_1 = 0;
            double count_2 = 0;
            double count_3 = 0;
            double count_4 = 0;
            double count_5 = 0;
            
            double tatal = 0;
            
            MathRandom a = new MathRandom();
            StringBuffer sb = new StringBuffer();
            for (i = 0; i <= 100000; i++)
            {
                sb.append(a.PercentageRandom());
            }
            
            char[] chars = sb.toString().toCharArray();
            for(i=0; i<chars.length; i++) {
                if(chars[i] == '0') {
                    count_0++;
                } else if(chars[i] == '1') {
                    count_1++;
                } else if(chars[i] == '2') {
                    count_2++;
                } else if(chars[i] == '3') {
                    count_3++;
                } else if(chars[i] == '4') {
                    count_4++;
                } else if(chars[i] == '5') {
                    count_5++;
                }
            }
            
            tatal = count_0+count_1+count_2+count_3+count_4+count_5;
            
            System.out.println("统计如下:");
            System.out.println("0的个数为:"+count_0+"---概率为"+count_0/tatal);
            System.out.println("1的个数为:"+count_1+"---概率为"+count_1/tatal);
            System.out.println("2的个数为:"+count_2+"---概率为"+count_2/tatal);
            System.out.println("3的个数为:"+count_3+"---概率为"+count_3/tatal);
            System.out.println("4的个数为:"+count_4+"---概率为"+count_4/tatal);
            System.out.println("5的个数为:"+count_5+"---概率为"+count_5/tatal);
            
        }
    }

    运行程序如下:

  • 相关阅读:
    9.4
    9.3
    9.2
    2016.9.1
    html,body
    prototype
    京东笔试题的一些小细节
    gulp安装过程中的问题。
    json
    双飞翼布局和圣杯布局
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4837443.html
Copyright © 2020-2023  润新知