Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中非均匀分布的随机数生成_Java_Random - Fatal编程技术网

Java中非均匀分布的随机数生成

Java中非均匀分布的随机数生成,java,random,Java,Random,我在java程序中工作,我需要为我的文件生成随机值 int age; for(int i=0;i<100;i++){ age=random.nextInt(20); //here i want 30% of 100 to be exactly equal to 15 , and the others are not important for me } int年龄; 对于(int i=0;i您可以根据需要修改分布数组。此方法返回100个随机数,分布为u public static L

我在java程序中工作,我需要为我的文件生成随机值

int age;
for(int i=0;i<100;i++){
age=random.nextInt(20);
//here i want 30% of 100 to be exactly equal to 15 , and the others are not important for me 

}
int年龄;

对于(int i=0;i您可以根据需要修改分布数组。此方法返回100个随机数,分布为u

public static List<Integer> generateRandom(){
    List<Integer> list = new ArrayList<Integer>();
    Random objGenerator = new Random();
    int distrubution[] = {0,0,0,0,1,1,1,2,3,4};
    for (int iCount = 0; iCount< 100; iCount++){
        int randomNumber = distrubution[objGenerator.nextInt(10)];
        list.add(randomNumber);
        System.out.println("Random No : " + randomNumber);
    }
    return list;
}
公共静态列表生成器dom(){
列表=新的ArrayList();
随机对象生成器=新随机();
int分布[]={0,0,0,1,1,1,2,3,4};
对于(int-iCount=0;iCount<100;iCount++){
int randomNumber=分布[objGenerator.nextInt(10)];
列表。添加(随机编号);
System.out.println(“随机编号:+randomNumber”);
}
退货清单;
}

您尝试过什么吗?请阅读java有哪些类可用于生成随机数,并查看这些类的文档?您能再介绍一下“etc”吗部分?20%之后是什么?这些频率应该是精确的还是统计平均值?来自非均匀分布的随机值仍然是随机值。他应该这样问。现在我明白了。我现在将更改答案。这是个好主意,但结果与我需要的百分比不完全相同,因为它是从e范围没有条件,我需要这个条件来获得我需要的精确的零数,但是谢谢你的帮助