33,025
社区成员




//用一个骰子从n件事中平均选出一种
//解决思路:6进制数字
package com.Jordan.Test;
import java.util.Random;
public class Try_10_DiceFor7 {
public static void main(String []args) {
int things = 7; //37件事需要平均
randomForSeven(things);
}
//先确定需要ROLL几次
public static int randomForSeven(int things) {
int times = 0; //需要ROLL的次数
int maxTimes = 7; //最多ROLL7次,7位6进制数字,很大了O.O
int total = 0; //得到的6进制数字最多能表示多少个10进制数字
int uselessNums = 0; //需要舍去的数字个数;
for(int i = 0; i < maxTimes; i++) {
times ++;
total = (int)Math.pow(6, i+1);
if(total >= things) {
System.out.println("一共需要ROLL"+times+"次~~~");
uselessNums = total - total/things*things;
break;
}
}
Random random = new Random();
int totalTimes = 0; //总共尝试的次数;
for(int i = 0; i < 50; i++) {
totalTimes ++;
int number = 0; //本次ROLL到的数字
int realNumber = 0; //在things范围内得到的有效数字;
for(int j = 0; j < times; j++) {
number += random.nextInt(5)*(int)Math.pow(6, j);
}
number ++;
if(number >= total - uselessNums) {
System.out.println("本次ROLL到的数字为" + number + "不是有效数字,重新来过~~");
continue;
} else {
realNumber = number % things + 1;
System.out.println("ROLL点成功,一共尝试了:" + totalTimes + "次,ROLL到的数字为:" + number + " 判定数字为:" + realNumber);
return realNumber;
}
}
return -1;
}
}