java概率,java概率取数组中的数

发布时间:2023-01-08

本文目录一览:

1、求一段JAVA的概率算法
2、java程序中概率问题
3、java中如何让A的输出的概率为50%B输出的概率为30%C输出的概率为20%
4、java中概率的问题

求一段JAVA的概率算法

public class Zhuq {
    public static void main(String[] args) {
        List<Person> listP = new ArrayList<Person>();
        listP.add(new Person("小李", "1", 200));
        listP.add(new Person("小王", "2", 210));
        listP.add(new Person("小赵", "3", 230));
        listP.add(new Person("小孙", "4", 100));
        listP.add(new Person("小钱", "5", 3));
        listP.sort(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return (((Person) o1).count) * (Math.random() * 10 + 1) > (((Person) o2).count) * (Math.random() * 10 + 1) ? -1 : 1;
            }
        });
        System.out.println(listP);
    }
}
class Person {
    String personName;
    String id;
    int count;
    public Person(String personName, String id, int count) {
        this.personName = personName;
        this.id = id;
        this.count = count;
    }
    @Override
    public String toString() {
        return "Person [personName=" + personName + ", id=" + id + ", count=" + count + "]";
    }
}

// 本质还是随机数

java程序中概率问题

用概率模型,先随机一次看取用哪个概率,随后再随机一次。代码示例如下:

import java.util.Random;
public class HelloWorld {
    public static void main(String[] args) {
        Random random = new Random();
        double p1 = 0.7; // 1~4的概率
        double p = (...

java中如何让A的输出的概率为50%B输出的概率为30%C输出的概率为20%

public class Test {
    public static void main(String[] args) {
        double d = Math.random(); // 生成一个0~1的随机数
        if (d <= 0.5) {
            System.out.println("A"); // 50%概率
        } else if (d <= 0.8) {
            System.out.println("B"); // 30%概率
        } else {
            System.out.println("C"); // 20%概率
        }
    }
}

java中概率的问题

你的问题描述不清。 如果是别的数字是均等的,那把一个单独处理,别的数字分享17/20的概率。实际上是一个映射的问题。具体实现就是拿20个数字做random,然后取整,比如1-1,2、3-2,若是其它,则重新获取一个3的random,当然要把1和2给去掉 —————————————— 那不就更容易了,剩下的不需要重新获取random了,直接就是3 ————————————————————————

public static void main(String arg[]) {
    System.out.println(getInt());
}
private static long getInt() {
    long a = Math.round(Math.random() * 20);
    if (a == 0 || a == 1) {
        return 1;
    } else if (a == 2) {
        return 2;
    } else {
        return 3;
    }
}