无路可走了,讨教一概率算法

0  50%

1  35%

2  15%

 

 

0 出现的概率为50%

1 出现的概率为35%

2 出现的概率为15%

 

在这3个数中按以上概率取其中的一个数。

 


问题补充:

0? 50%


1? 35%



2? 15%



?



?



0 出现的概率为50%



1 出现的概率为35%



2 出现的概率为15%



?



在这3个数中按以上概率取其中的一个数。



?



谢谢bohemia,刚才我也弄了一个,呵呵,过来一看差不多。呵呵。

public class RandomTest {
private static Random RANDOM = new Random();
public static void main(String[] args) {
    List list = new ArrayList();
    list.add(new Integer(25));
    list.add(new Integer(40));
    list.add(new Integer(35));
    Collections.sort(list);
    System.out.println(list);

    int a = ((Integer) list.get(0)).intValue();
    int b = ((Integer) list.get(1)).intValue();
    int c = ((Integer) list.get(2)).intValue();
    System.out.println("几率最大的数:" + c);
    System.out.println("中间数:" + (c + b));
    Map map = new HashMap();
    int x = 0;
    int y = 0;
    int z = 0;
    for (int i = 0; i < 1000000; i++) {
        int k = RANDOM.nextInt();
        int j = Math.abs(k % 100);
        if (j <= c) {
            map.put("0", new Integer(++x));
        } else if (j > c && j <= (c + b)) {
            map.put("1", new Integer(++y));
        } else if (j > (c + b) && j < (a+b+c)) {
            map.put("2", new Integer(++z));
        }

    }
    System.out.println(map);
}

}


问题补充:

0? 50%



1? 35%



2? 15%



?



?



0 出现的概率为50%



1 出现的概率为35%



2 出现的概率为15%



?



在这3个数中按以上概率取其中的一个数。



?


问题补充:

0  50%




1  35%




2  15%




 




 




0 出现的概率为50%




1 出现的概率为35%




2 出现的概率为15%




 




在这3个数中按以上概率取其中的一个数。




 




谢谢bohemia,刚才我也弄了一个,呵呵,过来一看差不多。呵呵。



public class RandomTest {

private static Random RANDOM = new Random();

public static void main(String[] args) {




List list = new ArrayList();

list.add(new Integer(25));

list.add(new Integer(40));

list.add(new Integer(35));

Collections.sort(list);

System.out.println(list);



int a = ((Integer) list.get(0)).intValue();

int b = ((Integer) list.get(1)).intValue();

int c = ((Integer) list.get(2)).intValue();

System.out.println("几率最大的数:" + c);

System.out.println("中间数:" + (c + b));

Map map = new HashMap();

int x = 0;

int y = 0;

int z = 0;

for (int i = 0; i < 1000000; i++) {

int k = RANDOM.nextInt();

int j = Math.abs(k % 100);

if (j <= c) {

map.put("0", new Integer(++x));

} else if (j > c && j <= (c + b)) {

map.put("1", new Integer(++y));

} else if (j > (c + b) && j < (a+b+c)) {

map.put("2", new Integer(++z));

}



}

System.out.println(map);

}



}


问题补充:

0? 50%



1? 35%



2? 15%



?



?



0 出现的概率为50%



1 出现的概率为35%



2 出现的概率为15%



?



在这3个数中按以上概率取其中的一个数。



?




谢谢bohemia,刚才我也弄了一个,呵呵,过来一看差不多。呵呵。



public class RandomTest {

private static Random RANDOM = new Random();

public static void main(String[] args) {




List list = new ArrayList();

list.add(new Integer(25));

list.add(new Integer(40));

list.add(new Integer(35));

Collections.sort(list);

System.out.println(list);



int a = ((Integer) list.get(0)).intValue();

int b = ((Integer) list.get(1)).intValue();

int c = ((Integer) list.get(2)).intValue();

System.out.println("几率最大的数:" + c);

System.out.println("中间数:" + (c + b));

Map map = new HashMap();

int x = 0;

int y = 0;

int z = 0;

for (int i = 0; i < 1000000; i++) {

int k = RANDOM.nextInt();

int j = Math.abs(k % 100);

if (j <= c) {

map.put("0", new Integer(++x));

} else if (j > c && j <= (c + b)) {

map.put("1", new Integer(++y));

} else if (j > (c + b) && j < (a+b+c)) {

map.put("2", new Integer(++z));

}



}

System.out.println(map);

}



}

[code="java"]public class TestRandom{
public static void main(String[] args){

    int a = 50;
    int b = 35;
    int c = 15;

    int a2 = 0;
    int b2 = 1;
    int c2 = 2;

    Map result = new LinkedHashMap();

    for(int i=0; i<a; i++){
        result.put(""+i,""+a2);
    } 
    for(int i=a; i<a+b; i++){
        result.put(""+i,""+b2);
    } 
    for(int i=a+b; i<a+b+c; i++){
        result.put(""+i,""+c2);
    } 

    System.out.println("Result:"+result);            

    System.out.println("随机数:"+result.get(""+(int)(Math.random()*100)));    

}

}[/code]

不知道行不行;

呵呵.学习参考;