请问大家 java 抽签算法可以怎么实现?
例如10个人里面选中5人,概率要平均
例如10个人里面选中5人,概率要平均
解决方案 »
- 怎样清理JAVA屏幕打印信息?
- 关于用Runtime.getRuntime().exec(cmd)等待一段时间后强制结束的问题
- JS 时钟问题
- 输出结果是什么?
- struts中<bean:write>标签现实问题--从库中读出字符串中的<被替换成了“&It;”;
- 谁给我分析一下这两段代码呀?谢谢哦~~
- Java中如何把日期存储到ACCESS的DATE/TIME字段中?
- 加入JAVA学习群,我们共同学习!
- \n是一个软回车,在我输出的txt文件里根本就没有真正换行!!!!怎么办?
- JSOUP CONNECTION RESPONSE COOKIES
- 程序什么地反有错
- 请教一下 Applet 播放音乐
你看看吧
#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();
}
把10个人装进一个list里然后用 (int) Math.round(Math.random()*9 生成 一个0-9的随机数按这个数选择list对应位置的对象再 (int) Math.round(Math.random()*8 生成0-8的随机数
按对应位置选择循环一直到选够5个人这个算法概率是不是均等的··~?
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();
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{
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个随即数
因为不是一个一个抽取所以应该具有公平性\
下面是代码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(); }}
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);
}
}
}
http://blog.chinaunix.net/u2/80678/showart_1315214.html下载那个chm 1.6的