请问大家 java 抽签算法可以怎么实现?
例如10个人里面选中5人,概率要平均

解决方案 »

  1.   

    LZ,哎,,,文采不好,述说不清楚。只好发个C程序给你咯
    你看看吧
    #include "stdio.h"
    #include "stdlib.h"
    struct node{
    char flag;
    int num;
    };main()
    {
    struct node list[10]={
    {1,1},{1,2},{1,3},
    {1,4},{1,5},{1,6},
    {1,7},{1,8},{1,9},
    {1,10}
         };
    int n=0,count,j;
    int len=sizeof(list)/sizeof(list[0]);
    randomize();
    while(n<6)
    {
    int temp=random(len-n)+1;
    for(j=0,count=0;j<len;j++)
    {
    count+=list[j].flag;
    if(count==temp)
    {
    printf("%d  ",list[j].num);
    list[j].flag=0;   /*该号码已选出*/
    break;
    }
    }
    n++;
    }
    printf("\n");
    getch();
    }
      

  2.   

    我的算法是
    把10个人装进一个list里然后用 (int) Math.round(Math.random()*9 生成 一个0-9的随机数按这个数选择list对应位置的对象再 (int) Math.round(Math.random()*8 生成0-8的随机数
    按对应位置选择循环一直到选够5个人这个算法概率是不是均等的··~?
      

  3.   

    最土的办法,加个标识位。你可以进一步加一些代码来验证这些被取出来的数是不是随机的,也就是它们是不是均匀分布的。我一共循环了100次,每次取出5个数,也就是一共取出了500个数,而一共有10个数,所以每个数最后被取出的总和近视等于50。
    import java.util.*;public class RandomTen {
    public static void main(String args[]){
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    int seed = (int)(Math.random()*9 + 1);
    int count = 0;

    for (int i=0; i<10; i++)
    map.put(i, 0); //以0标识该元素未被选中

    for(int i=0; i<100; i++){
    while ((count != 5)){
    if (map.get(seed) == 0){
    System.out.print(seed +" ");
    count++;
    map.remove(seed);
    map.put(seed, 1); //选中即标识为1
    }

    seed = (int)(Math.random()*9 + 1);
    }

    count = 0;

    map.clear(); //重新初始化
    for (int j=0; j<10; j++)
    map.put(j, 0);

    System.out.println();
    }
    }
    }
    结果:
    1 6 9 3 4 
    5 2 9 7 6 
    2 7 1 3 5 
    4 8 5 9 7 
    4 9 3 8 2 
    4 6 1 8 7 
    3 8 2 1 5 
    2 3 4 9 6 
    4 1 3 9 8 
    6 5 3 2 4 
    6 2 3 5 7 
    8 3 5 1 2 
    7 9 6 8 2 
    4 1 3 7 5 
    8 3 4 7 1 
    7 6 3 9 8 
    1 2 9 7 3 
    3 6 9 7 4 
    5 9 3 2 8 
    4 3 6 2 5 
    9 1 8 4 6 
    4 3 6 8 2 
    6 8 9 2 3 
    3 8 5 9 6 
    7 2 4 9 6 
    3 7 2 8 9 
    4 7 6 8 1 
    9 6 4 8 1 
    7 8 2 5 6 
    1 4 8 7 3 
    7 9 2 4 6 
    1 4 7 5 6 
    1 8 5 6 3 
    4 3 9 1 7 
    9 2 8 4 6 
    7 1 5 4 6 
    4 1 9 5 6 
    1 7 2 3 4 
    2 1 8 4 9 
    6 9 7 3 8 
    6 3 8 7 4 
    9 8 2 1 3 
    7 8 2 4 3 
    2 5 7 4 1 
    5 7 6 8 9 
    2 8 5 7 9 
    5 7 6 3 2 
    7 5 2 1 6 
    5 2 9 3 8 
    8 9 1 5 7 
    6 4 9 7 5 
    3 6 8 1 5 
    1 3 6 8 7 
    9 6 1 3 8 
    6 4 5 8 2 
    8 9 7 5 4 
    4 2 8 9 5 
    8 2 6 5 3 
    7 1 5 6 4 
    1 3 5 7 2 
    1 3 5 7 4 
    3 5 8 7 6 
    2 6 9 3 8 
    8 4 1 6 9 
    1 3 9 6 7 
    5 1 7 2 6 
    3 7 1 5 2 
    3 9 5 6 8 
    1 4 8 6 9 
    7 5 2 3 9 
    1 8 6 4 9 
    4 7 3 2 9 
    9 2 4 3 5 
    4 6 3 1 9 
    7 2 6 4 5 
    3 9 4 5 2 
    4 2 5 8 7 
    6 1 8 7 4 
    8 2 1 6 3 
    6 2 8 1 4 
    5 4 2 6 1 
    5 3 7 9 2 
    4 9 1 8 3 
    9 8 7 1 4 
    2 7 3 9 8 
    4 2 8 3 5 
    5 8 7 2 6 
    6 8 3 4 9 
    7 2 3 9 4 
    9 5 7 2 1 
    3 7 2 6 9 
    2 3 5 1 7 
    9 4 1 3 6 
    2 4 1 7 8 
    1 9 7 3 4 
    3 5 6 9 1 
    8 3 7 1 2 
    4 1 6 2 5 
    3 9 4 8 1 
    8 9 4 1 6 
      

  4.   

    不好意思,5#的代码有点小问题,有没有发现没有选中0啊?是我的那个seed搞错了,改成: seed = (int)(Math.random()*10)记好了。看下面的代码,我加了一个统计数组,然后把默认的输出注释掉了,这样你就能看见是不是均匀输出了:
    import java.util.*;public class RandomTen {
    public static void main(String args[]){
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    int[] statistics = new int[10];

    int seed = (int)(Math.random()*10);
    int count = 0;

    for (int i=0; i<10; i++)
    map.put(i, 0); //以0标识该元素未被选中

    for(int i=0; i<100; i++){
    while ((count != 5)){
    if (map.get(seed) == 0){
    //System.out.print(seed +" ");
    count++;
    statistics[seed]++;
    map.remove(seed);
    map.put(seed, 1); //选中即标识为1
    }

    seed = (int)(Math.random()*10);
    }

    count = 0;

    map.clear(); //重新初始化
    for (int j=0; j<10; j++)
    map.put(j, 0);

    //System.out.println();
    }

    for (int i=0; i<10; i++)
    System.out.print(statistics[i] +" ");
    }
    }结果:
    47 50 48 52 43 55 43 51 54 57 每个数基本都是被调了50次,和理论值差不多。如果你循环的越多,这个实际值和理论值越接近。
      

  5.   

    或者楼主在简化一下我的代码,直接把那个HashMap也删了,用数组来搞,省空间,有效率。这个我就不改了,楼主自己来吧。
      

  6.   

    这么说(int)(Math.random()*10)得到的随机数概率是比较平均的?
      

  7.   

    用Random产生5个随即数
    在判断是否有重复的
    若有则重新产生5个随即数
    因为不是一个一个抽取所以应该具有公平性\
    下面是代码import java.util.HashSet;
    import java.util.*;
    import java.util.Random;
    public class Sortition {
    Random r = new Random();
    int a;
    Set s = new HashSet();
    public void getit() {
    for(int i=0; i<5; i++) {//随机5次产生数字,可能产生相同的
    a = r.nextInt(9)+1;
    s.add(a);//加到Set容器里面去,如果有相同的则容器的长度小于5
    }
    if(s.size()<5) {//若小于5则重做
    getit();
    }else {
    System.out.print(s);
    }
    }  public static void main(String[] args) {
    new Sortition().getit(); }}
      

  8.   


    import java.util.ArrayList;
    public class RandomTest {
    public static void main(String[] args) {
    for(int j=0; j<100; j++) {
    ArrayList<Integer> arr = new ArrayList<Integer>();
    for(int i=0; i<10; i++) {
    arr.add(i);
    }
    choose(arr, 5);
    System.out.println();
    }
    }
    // 抽签算法
    static void choose(ArrayList<Integer> arr, int count) {
    for(int i=0; i<count; i++) {
    int rand = (int) (Math.random()*(arr.size()));
    System.out.print(arr.get(rand) + " ");
    arr.remove(rand);
    }
    }
    }
      

  9.   

    下载一个API文档看一下,会给你解释的。
    http://blog.chinaunix.net/u2/80678/showart_1315214.html下载那个chm 1.6的