# 按一定概率产生随机数的有关问题！

www.MyException.Cn  网友分享于：2013-02-26  浏览：61次

------解决方案--------------------

------解决方案--------------------

int point = (int)(Math.random()*100);
if (point<=55) return 0;
else if (point<=75) return 1;
...
------解决方案--------------------

Java code
```
import java.util.Random;
/**
*
* @author CSDN - cybio
* Date 2010-12-30
*/
public class Test02 {

//统计出现概率的计数变量
int count_0, count_1, count_2, count_3; // 4种选择结果

Random r = new Random();    //随机数生成器

//评估函数: 计算运行不同的次数 , 每种结果的出现概率
public void calc(int count) {
int num;
for(int i=0; i<count; i++) {
num = r.nextInt(100) + 1;    //让随机数在1~100间产生随机数
if(num <= 55) {        //55%
count_0++;
} else if(num <= 75) {        //20%
count_1++;
} else if(num <= 95) {        //20%
count_2++;
} else if(num <= 100) {        //5%
count_3++;
}
}
System.out.println("运行 " + count + " 次的结果为:");
System.out.println("0 出现概率: " + ((float)count_0/count*100) + "%");
System.out.println("1 出现概率: " + ((float)count_1/count*100) + "%");
System.out.println("2 出现概率: " + ((float)count_2/count*100) + "%");
System.out.println("3 出现概率: " + ((float)count_3/count*100) + "%");

//计数器清零
count_0 = count_1 = count_2 = count_3 = 0;
}

public static void main(String[] args) {
Test02 t = new Test02();
t.calc(100);
t.calc(200);
t.calc(500);
t.calc(1000);
}

}```