JAVA中怎么实现一个概率计算

发布时间:2021-07-01 15:30:12 作者:Leah
来源:亿速云 阅读:1205

JAVA中怎么实现一个概率计算,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

程序中经常遇到随机送红包之类的情景,这个随机还得指定概率,比如10%的机率可以得到红包。那么java怎么实现一个简单的概率计算了,见如下例子:

int randomInt =  RandomUtils.nextInt(1,101);if(randomInt <= 10){ //100里面1个数,小于等于10的概率就是10%       //do something}

RandomUtils工具类是commons-lang3包里面的

<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.7</version></dependency>

 如果要在某个数字区间产生一个随机数,区间内部在不同的片段几率不同如何实现呢?经常有这样的场景,比如,随机赠送红包,范围0.1元-100元,0.1-1元的概率是90%,1元-10元的概率是9%,10元-100元的概率是1%,也就是说数额越大得到的几率越小!实现的原理如下图:

JAVA中怎么实现一个概率计算

原理就是,将范围分割成一个个子范围(片段),具体采用哪个范围,再用机率判断。片段机率可以依次排好序,映射成[1,100]之间的数字。然后随机一个[1,100]之间的数,该数落在哪个区间,就采用哪个片段产生随机数。具体源代码如下:

package com.jeeplus.common.utils;import org.apache.commons.lang3.RandomUtils;import java.util.ArrayList;import java.util.List;/** * 按几率产生随机数 * 例如,产生0.1-100的随机数,0.1-1的几率是90%,1-10的几率是9%,10-100的几率是1% */public class RateRandomNumber {/**     * 产生随机数     * @param min 最小值     * @param max 最大值     * @return 随机结果     */    public static double produceRandomNumber(double min,double max){return RandomUtils.nextDouble(min,max); //[min,max]    }/**     * 按比率产生随机数     * @param min 最小值     * @param max 最大值     * @param separates 分割值(中间插入数)     * @param percents 每段数值的占比(几率)     * @return 按比率随机结果     */    public static double produceRateRandomNumber(double min,double max,List<Double> separates,List<Integer> percents){if(min > max){throw new IllegalArgumentException("min值必须小于max值");
        }if(separates == null || percents==null || separates.size()==0){return produceRandomNumber(min,max);
        }if(separates.size() +1 != percents.size()){throw new IllegalArgumentException("分割数字的个数加1必须等于百分比个数");
        }int totalPercent = 0;for(Integer p:percents){if(p<0 || p>100){throw  new IllegalArgumentException("百分比必须在[0,100]之间");
            }
            totalPercent += p;
        }if(totalPercent != 100){throw new IllegalArgumentException("百分比之和必须为100");
        }for(double s:separates){if(s <= min || s >= max){throw new IllegalArgumentException("分割数值必须在(min,max)之间");
            }
        }int rangeCount = separates.size()+1; //例如:3个插值,可以将一个数值范围分割成4段        //构造分割的n段范围        List<Range> ranges = new ArrayList<Range>();int scopeMax = 0;for(int i=0;i<rangeCount;i++){
            Range range = new Range();
            range.min = (i==0 ? min:separates.get(i-1));
            range.max = (i== rangeCount-1 ?max:separates.get(i));
            range.percent = percents.get(i);//片段占比,转换为[1,100]区间的数字            range.percentScopeMin = scopeMax +1;
            range.percentScopeMax = range.percentScopeMin + (range.percent-1);
            scopeMax = range.percentScopeMax;

            ranges.add(range);
        }//结果赋初值        double r = min;int randomInt = RandomUtils.nextInt(1,101); //[1,100]        for(int i=0;i<ranges.size();i++){
            Range range = ranges.get(i);//判断使用哪个range产生最终的随机数            if(range.percentScopeMin <= randomInt && randomInt <= range.percentScopeMax){
                r = produceRandomNumber(range.min,range.max);break;
            }
        }return r;
    }public static class Range{public double min;public double max;public int percent; //百分比        public int percentScopeMin; //百分比转换为[1,100]的数字的最小值        public int percentScopeMax; //百分比转换为[1,100]的数字的最大值    }public static void main(String[] args) {
        List<Double> separates = new ArrayList<Double>();
        separates.add(1.0);
        separates.add(10.0);
        List<Integer> percents = new ArrayList<Integer>();
        percents.add(90);
        percents.add(9);
        percents.add(1);for(int i=0;i<100;i++) {double number = produceRateRandomNumber(0.1, 100, separates, percents);
            System.out.println(String.format("%.2f",number));
        }
    }
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. 在线抽奖大转盘和概率计算
  2. 使用Python怎么计算彩票的概率

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:使用Java怎么连接Redis服务器

下一篇:计算机网络中需要了解哪些办公软件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》