本帖最后由 daifeng4325416 于 2010-12-29 20:30:34 编辑

解决方案 »

  1.   


    import java.util.*;
    public class Gailv
    {
    public static void main(String[] args)
    {
    Random rand=new Random(47);
    int sum=1000;
    List<Integer> list=new ArrayList<Integer>();
    for(int i=0;i<sum;i++)
    {
    int r=rand.nextInt(4);
      if(r==0 && r<550)     //55%
      list.add(r);
      if(r==1 && r<200)     //20%
      list.add(r);
      if(r==2 && r<200)     //20%
      list.add(r);
      if(r==3 && r<50)      //5%
      list.add(r);
    }

    }
    }楼主是要这样的?
      

  2.   

    数组只是为了方便。
    其实像100个元素这样的情况下,根据返回结果的范围,用if判断一下就行了,如int point = (int)(Math.random()*100);
    if (point<=55) return 0;
    else if (point<=75) return 1;
    ...
      

  3.   

    哦 还得加个返回循环的 否则add方法重复做了。
    不过思路应该这样子。
      

  4.   


    import java.util.*;
    public class Gailv
    {
    public static void main(String[] args)
    {
    Random rand=new Random(47);
    int sum=100;
    int s=0;
    int m=0;
    int k=0;
    int l=0;
    List<Integer> list=new ArrayList<Integer>();
    for(int i=0;i<sum;i++)
    {
    int r=rand.nextInt(4);
      if(r==0 )
      { 
       if(s<50){list.add(r);s++;}
       if(s>50)                      
       {
       r=rand.nextInt(4);
       if(r==0) i--;
       }
      
      }
      if(r==1 )
      { 
       if(m<20){list.add(r);m++;}
       if(m==20)
       {
       r=rand.nextInt(4);
       if(r==0 || r==1) i--;
       }
        
      }
      if(r==2 )
      { 
       if(k<20){list.add(r);k++;}
       if(k==20)
       {
       r=rand.nextInt(4);
       if(r==0 || r==1 || r==2) i--;
       }
      
      
      }
      if(r==3) 
      { 
       if(l<10){list.add(r);l++;}
       if(l==10)
       {
       r=rand.nextInt(4);
       i--;
       }
      
      
      }
      
    }
    System.out.println(list);
    }
    }分别是50% 20% 20% 10%囧,不会求list指定元素的数量。
    运行结果:
    [2, 1, 2, 0, 0, 2, 0, 1, 2, 2, 1, 3, 1, 0, 0, 2, 3, 0, 3, 2, 2, 0, 2, 1, 1, 0, 3
    , 0, 0, 3, 2, 1, 3, 0, 1, 2, 0, 0, 1, 3, 1, 1, 0, 3, 3, 1, 2, 0, 2, 3, 2, 2, 0,
    1, 1, 2, 2, 0, 1, 0, 1, 2, 1, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
      

  5.   


    import java.util.*;
    public class Gailv
    {
    public static void main(String[] args)
    {
    Random rand=new Random(47);
    int sum=100;
    int s=0;
    int m=0;
    int k=0;
    int l=0;
    List<Integer> list=new ArrayList<Integer>();
    for(int i=0;i<sum;i++)
    {
    int r=rand.nextInt(4);
      if(r==0 )
      { 
       if(s<50){list.add(r);s++;}
       if(s>50)
       {
       r=rand.nextInt(4);
       if(r==0) i--;
       }
      
      }
      if(r==1 )
      { 
       if(m<20){list.add(r);m++;}
       if(m==20)
       {
       r=rand.nextInt(4);
       if(r==0 || r==1) i--;
       }
        
      }
      if(r==2 )
      { 
       if(k<20){list.add(r);k++;}
       if(k==20)
       {
       r=rand.nextInt(4);
       if(r==0 || r==1 || r==2) i--;
       }
      
      
      }
      if(r==3) 
      { 
       if(l<10){list.add(r);l++;}
       if(l==10)
       {
       r=rand.nextInt(4);
       i--;
       }
      
      
      }
      
    }
    for(int i=0;i<100;i++)

    {
    int p=rand.nextInt(100);
    System.out.print(list.get(p)+",");
    }

    }
    }
      

  6.   

    晕 啊 做一步 就发现自己错一步这题最简单的方法就是按照概率算出各个数字的多少,
    用循环添加到list中,
    最后随机取list下标为0-99的值。
      

  7.   

    下边这个方法可以一参数的形式用任何概率public class RandomNum { public static void main(String[] args) {
        System.out.println( getRandomNum(new int[]{0,1,2,3}, new int[]{50,20,20,10}));
    }

    //probability 与 arr 一一对应的表示 arr 中各个数的概率,且满足 probability 各元素和不能超过 100;
    public static int getRandomNum(int[] arr, int[] probability){
    if(arr.length != probability.length) return Integer.MIN_VALUE;
    Random ran = new Random();
    int ran_num = ran.nextInt(100);
    int temp = 0;
    for (int i = 0; i < arr.length; i++) {
    temp += probability[i];
    if(ran_num < temp)
    return arr[i];
    }
    return Integer.MIN_VALUE;
    }
    }
      

  8.   

    先随机产生一个随机数a 如1到100之间
    定义变量b 如果想让b=0的概率为25%,可以判断当a在1到25之间 就给b赋值0
    其他依次类推。
      

  9.   

    我也来试试package pk;import java.util.Random;/**
     * @author admin
     * 
     */
    public class TestRandom { /**
     * @param args
     */
    public static void main(String[] args) { TestRandom tRandom = new TestRandom();
    int n = 0;
    int n0 = 0, n1 = 8, n2 = 0, n3 = 0; for (int i = 0; i < 1000; i++) {
    n = tRandom.getNum(); switch (n) {
    case 0:
    n0++;
    break; case 1:
    n1++;
    break; case 2:
    n2++;
    break; case 3:
    n3++;
    break;
    }
    }

    System.out.println("n0 : " + n0);
    System.out.println("n1 : " + n1);
    System.out.println("n2 : " + n2);
    System.out.println("n3 : " + n3);
    } /**
     * get a number.
     * 
     * @return number
     */
    public int getNum() {
    Random random = new Random(); int n = random.nextInt(100);
    int result; if (n < 50) {
    result = 0;
    } else if (n >= 50 && n < 70) {
    result = 1;
    } else if (n >= 70 && n < 90) {
    result = 2;
    } else {
    result = 3;
    }
    return result;
    }}
      

  10.   

    int base = 1000000000;
    Random rd = new Random(0, base); 
    int[,2] a = {{0,5000000000},{1,2000000000},{2,2000000000},{3,1000000000}};int n = 100;
    for(int i=0;i<n;i++)
    {
        int r = rd.Next();
        int sum = 0;
        for(int j =0;j<a.Length;j++)
        {
            sum+=a[j][1];
            if(r<=sum)
            {
                //这次选中的是a[j][0];
                break;
            }
        }
    }
      

  11.   


    /*
    我想在{0,1,2,3}中随机产生一个数,产生0的概率为50%,1为20%,2为20%,3为10%,请问怎样的才能高效地实现呢?
    */
    import java.util.Random;
    import java.math.BigDecimal;public class Test{
    public static void main(String[] args){
    int[] array = {0,1,2,3};
    double[] odds = {0.5,0.2,0.2,0.1};
    int result = getEume(array,odds);
    System.out.println(result);
    } public static int getEume(int[] array,double[] odds){
    int result = 10000;
    if(array.length != odds.length){
    throw new IllegalArgumentException("Illegal Argument! the length of array and the length of odds is not equals.");
    } if(merge(odds) != 1.0){
    throw new IllegalArgumentException("Illegal Argument!merge odds ,the result is not 100%. ");
    } Random random = new Random(); double d = random.nextDouble(); System.out.println(d); double left = 0.0;
    double right = 0.0; for(int i = 0 ; i < odds.length ; i ++){
    right = left + odds[i];
    //System.out.println();
    if(d >= left && d < right){
    result = array[i];
    break;
    }
    left = right;
    } return result; 
    } private static double merge(double[] odds){ double result = 0.0;
    for(double d : odds){
    result += d;
    } BigDecimal b = new BigDecimal(result);
    b = b.setScale(2,BigDecimal.ROUND_HALF_UP);
    result = b.doubleValue(); return result;
    }
    }
      

  12.   

    我要的概率是55%,20%,20%,5% {0,1,2,3}
    生成0-1之间的数numnum<=0.55   则生成 0
    0.55<num<=0.55+0.2   则生成 1
    0.75<num<=0.55+0.2   则生成 2
    0.95<num<=1   则生成 3
      

  13.   

    楼主 ,给你写了一个测试函数. java的Random类可以满足你的需要.
    代码里写了一个评估函数, 参数自己随便写. 运行多少次 概率都基本准确.不给分对不起我的辛苦啊 呵呵
    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);
    }}
      

  14.   


    Random ran = new Random();
            int number = ran.nextInt(100) + 1;//1-100
            int result = 0;
            if(number > 95)
             result = 3;
            else if(number > 75)
             result = 2;
            else if(number > 55)
             result = 1;
            else
             result = 0;
      

  15.   


    算法,因为计算机中的随机数,按理论上讲都是同概率的伪随机数,因此,每个数被选中的概率是一样的,
    因此我们可以设定一个基数,这个基数为10亿,也就是说,概率可以控制到10亿分之一,
    因此a数组中0出现的概率为 5亿次(针对10亿来说),也就是50%的概率,
    出现1和2的概率都为2亿次,因此也就是20%的概率
    出现3的次数为1亿次,因此也就是10%的概率当你要设计10亿分之1概率时,只要调整一下,后面的权值为1就可以了,如:
    int[,2] a = {{0,5000000000},{1,2000000000},{2,2000000000},{3,99999999},{4,1}};
    那么出现4的概率就是为10亿分之1    int r = rd.Next();
        int sum = 0;
        for(int j =0;j<a.Length;j++)
        {
            sum+=a[j][1];
            if(r<=sum)
            {
                //这次选中的是a[j][0];
                break;
            }
        }
    因此,随机一个0到10亿的数。
    如果这个数是小于5亿,则表示0被选中,
    否则,这个权值累加起来存到sum中
    然后第二次循环的时候,再判断这个数是否是小于Sum(此时的sum=5亿+2亿),如果小于,说明1被选中,
    以此类推
    该算法建立在,伪随机的理论之上(任何数出现的概率都是一样)
      

  16.   

    5楼的是一种方法,还有一种方法就是,产生0-9,10个随机数
    x<5 --> a=0
    x==5 or x==6 --> b=1
    x==7 or x==8 -->  c=2
    x==9 --> d=3
      

  17.   

    5楼的是一种方法,还有一种方法就是,产生0-9,10个随机数
    x<5 --> a=0
    x==5 or x==6 --> b=1
    x==7 or x==8 --> c=2
    x==9 --> d=3
    这个不错
      

  18.   

    楼的是一种方法,还有一种方法就是,产生0-9,10个随机数
    x<5 --> a=0
    x==5 or x==6 --> b=1
    x==7 or x==8 --> c=2
    x==9 --> d=3
    这个不错
      

  19.   

    按概率生成离散型随机数,又称为表随机数。是特定的一种。其实一楼提供方法显然可以适应于一定的场合,但是实际上并不通用。试想想看,如果其中有一个概率很小,那怎么办?比如万分之一,或亿分之一这样的数,那数组中要存多少的东西?其实最简单的方法是这样楼主的题目是:生成函数规则是
    0为50%,1为20%,2为20%,3为10%那么将比例变成累积概率即
    0对应[0.0-0.5],即Cp[0]=0.5;
    1对应(0.5-0.7],即Cp[1]=0.7;
    2对应(0.7-0.9],即Cp[2]=0.9;
    2对应(0.9-1.0],即Cp[3]=1.0;这样只要生成[0, 1]均匀分布的随机数,再与CP就行比较就可以了。比较方式如下:p=uniform(0);                         //0,1 均匀分布随机数,类似的函数很多库中都有
                                          //如果没有自己写一下也很简单
    for (int i=0; i<4; i++) 
      if (p<Cp[i]) return i;
      

  20.   

    请把p<Cp[i]
    改成p<=Cp[i]
      

  21.   

    http://hi.baidu.com/searcher_go_go/blog/item/3fe8923b90e030d37d1e718e.html。
      

  22.   

    package com.util.random;import java.util.Random;public class Rate {
    //在{0,1,2,3}中随机产生一个数,产生0的概率为55%,1为20%,2为20%,3为5%
    public static void TestRatedNumber(int num){
    Random r = new Random();
    int tem,i0=0,i1=0,i2=0,i3=0,t=0,j=0;
    for(int i=0;i<num;i++){
    tem = r.nextInt(100);
     t = tem<55?0:(tem<75?1:(tem<95?2:3));
     j = t==0?++i0:(t==1?++i1:(t==2?++i2:(t==3?++i3:i3)));
    }
    System.out.println(num+"个数的测试结果:");
    System.out.println("0出现的机率为"+(float)i0/num);
    System.out.println("1出现的机率为"+(float)i1/num);
    System.out.println("2出现的机率为"+(float)i2/num);
    System.out.println("3出现的机率为"+(float)i3/num);
    }
    public static void main(String[] args) {
    TestRatedNumber(100000);
    }
    }