如何去随机拆分100成n份,比如 [20,13,30,37]或[58,42]或[11,1,57,31]等等

流子
游戏开发领域优质创作者
博客专家认证
2009-09-15 05:46:05
如题
如何去随机拆分100成n份,比如 [20,13,30,37]或[58,42]或[11,1,57,31]等等
用java去实现一个方法,返回时int[]
...全文
1317 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
abc130314 2009-09-17
  • 打赏
  • 举报
回复
上面的很多算法都是不行的。因为当拆分的份数越来越多的时候,最后生成的数组的 第一位=1 和 最后一位=1 的概率相差就越来越大。除非在生成的数组后面做一个乱序的处理。

同时,还有一些问题。拿9L的来说,int n = r.nextInt(101);
当sum=10的时候,因为随机的还是0~100的整数,那这时候,随机生成的数中10~100之间的数,因为都>=10,所以最后的结果只能是10。而这时候,结果=10和 结果=小于10的某一个数的概率就完全不等了。

所以这样的算法完全不行。如果要改的话,应该是 int n = r.nextInt(sum)+1;

我的想法是。随机生成一个区间的数,然后把这个数随机的加到数组中的某一个数上面。
    private static int[] getRandomArray(int size) {
int[] percent = new int[size];
Arrays.fill(percent, 1);//保证最小的数==1
int s = 100 - size;
int k = 5;//常数,==1比较平均的分配,越大越不平均。
Random r = new Random();
while (s > 0) {
int x = r.nextInt(size);
int y = r.nextInt(k > s ? s : k) + 1;
percent[x] += y;
s -= y;
}
return percent;
}
haojia0716 2009-09-17
  • 打赏
  • 举报
回复
那就这样


public static void go() {
Random r = new Random();
List<Integer> list = new ArrayList<Integer>();
int sum = 0;
while (true) {
int t = 0;
while (t == 0) {
t = r.nextInt(100);
}
sum += t;
if (sum == 100) {
list.add(t);
System.out.println(list);
return;
}
if (sum < 100) {
list.add(t);
}
if (sum > 100) {
sum = 0;
list.clear();
}
}
}

public static void main(String[] args) {
for (int i = 0; i < 10; i++)
go();
}
GG_wg 2009-09-16
  • 打赏
  • 举报
回复
思路:
随机4个数,最后一个拿100减去这个数的和;
避免大于100,可以这样产生随机数,第一个数是100以内,第二数是在100减去第一个数的差内产生,第三个数是100减去前两个数的差内产生,一次类推,最后一个是100减去前四个数的差

zrcvic 2009-09-16
  • 打赏
  • 举报
回复
不用那么麻烦,可以这样做:

public int[] f(int total, int n) {

if (total < 0 || n <= 0) {
throw new IllegalArgumentException();
}

int[] x = new int[n];
Random r = new Random();

for (int i = 0; i < n - 1; i++) {
x[i] = r.nextInt(total + 1);
total -= x[i];
}
x[n - 1] = total;

return x;
}

另:6 楼的方法是不行的,因为这样生成的几个数总有 n - 1 个是小于等于 total / n 的,但这不是所有的情况,比如 [ 1, 33, 33, 33 ] 就不能用 6 楼的方法生成出来。
Sleeping0804 2009-09-16
  • 打赏
  • 举报
回复
学习...........
sforiz 2009-09-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 goblin_ssd 的回复:]
Java codepublicstaticvoid main(String[] args){
Random r=new Random();//r用来产生一个0-100的随机数。 List<Integer> list=new ArrayList<Integer>();//因为不知道要被分成几分,用List更好些。boolean flag=true;int sum=100¡­
[/Quote]

这个比较容易理解。
24K純帥 2009-09-16
  • 打赏
  • 举报
回复
我以前看过,用递归做的。。
snowmansh 2009-09-16
  • 打赏
  • 举报
回复
拆完后,所有数字之和应该确保还要是100吧?
abc130314 2009-09-16
  • 打赏
  • 举报
回复 1

private static int[] getRandomArray(int size) {
int[] percent = new int[size];
int s = 100;
double k = 0.6;//波动常数,位于0~1之间。如果=0,平均分配;=1,最大波动
for (int i = 0; i < size - 1; i++) {
double x = s / (size - i);
double y = Math.random() - 0.5;
double z = x + (y > 0 ? s - x : x) * y * k;
percent[i] = (int) z;
s -= percent[i];
}
percent[size - 1] = s;
return percent;
}
andyan_2008 2009-09-16
  • 打赏
  • 举报
回复
顶 9楼写的不错
haojia0716 2009-09-16
  • 打赏
  • 举报
回复
这个 刚写的

public static int[] go() {
Random r = new Random();
List<Integer> list = new ArrayList<Integer>();
int sum = 0;
while (true) {
int t = 0;
while (t == 0) {
t = r.nextInt(100);
}
sum += t;
if (sum < 100) {
list.add(t);
}
if (sum == 100) {
list.add(t);
int[] a = new int[list.size()];
for (int i = 0; i < a.length; i++) {
a[i] = list.get(i);
}
return a;
}
if (sum > 100) {
sum -= t;
}
}
}

public static void main(String[] args) {
System.out.println(Arrays.toString(go()));
}
gutan_fox 2009-09-15
  • 打赏
  • 举报
回复
9l的比较简单点
struggle_tomorrow 2009-09-15
  • 打赏
  • 举报
回复
import java.util.*;
class Chaifen
{
int n;
public Chaifen(int n)
{
this.n = n;
}
public int[] chaifen()
{
int[] a = new int[n];
Random rand = new Random();
int shengyu = 100;
for (int i=0;i<n-1;i++)
{
a[i] = rand.nextInt(shengyu);
shengyu = shengyu - a[i];
}
a[n-1] = shengyu;
return a;
}
public static void main(String[] args)
{
Chaifen ch = new Chaifen(5);
int[] b = ch.chaifen();
for (int i=0;i<b.length;i++)
{
System.out.println(b[i]);
}
}
}
closewbq 2009-09-15
  • 打赏
  • 举报
回复

import java.util.Stack;
public class Test {
private static Stack<Integer> array = new Stack<Integer>();
public static void main(String[] args) {
caifen(100, 0);
}
public static void caifen(int n, int yushu) {
if (n == 0) {
System.out.println(array);
return;
}
for (int i = yushu + 1; i <= n; i++) {
array.push(i);
caifen(n - i, i);
array.pop();
}
}
}
liang8305 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 chosen0ne 的回复:]
算法:初始只有一个概率为1,则将1分成两个概率;然后再在这两个概率中选择一个进行拆分,分成3份,直到所要求的份数。
e.g.  将100分成3份:初始[1],第一次迭代:只能选择1进行拆分,[0.3,0.7]
                          第二次迭代:随机选择一个已有概率进行拆分,[0.3,0.4,0.3]
                          完成。将每个概率乘以100就是结果。
[/Quote]


顶这个~~~~~~~!!!!!!!!!!!!!!
流子 2009-09-15
  • 打赏
  • 举报
回复
各位有什么更好的方法,期盼跟帖
流子 2009-09-15
  • 打赏
  • 举报
回复
我的实现如下,基本和楼上实现类似:
private int[] getRandomArray(int size) {
int[] percent = null;
if (size > 0) {
percent = new int[size];
if (1 == size) {
percent[0] = 100;

} else {
int i = 0;
int num = 100;
while (true) {
percent[i] = RandomUtils.nextInt(101);
num -= percent[i];
if (num == 0) {
break;
} else {
if ((num < 0) || (i == size - 1)) {
percent[i] += num;
break;
}
}
i++;
}
}
}
return percent;
}
Goblin_SSD 2009-09-15
  • 打赏
  • 举报
回复


public static void main(String[] args){
Random r = new Random();//r用来产生一个0-100的随机数。
List<Integer> list = new ArrayList<Integer>();//因为不知道要被分成几分,用List更好些。
boolean flag = true;
int sum = 100;
while(flag){
int n = r.nextInt(101);
if(n==0){
continue;
}else if((sum-n)>0){
list.add(n);
sum = sum-n;
}else if((sum-n)==0){
list.add(n);
flag = false;
}else if((sum-n)<0){
continue;
}
}
for(Integer a:list){
System.out.println(a);
}
}

chosen0ne 2009-09-15
  • 打赏
  • 举报
回复
算法:初始只有一个概率为1,则将1分成两个概率;然后再在这两个概率中选择一个进行拆分,分成3份,直到所要求的份数。
e.g. 将100分成3份:初始[1],第一次迭代:只能选择1进行拆分,[0.3,0.7]
第二次迭代:随机选择一个已有概率进行拆分,[0.3,0.4,0.3]
完成。将每个概率乘以100就是结果。
酒剑仙 2009-09-15
  • 打赏
  • 举报
回复
生成随机数啊
100 分 5份
100 生成0-100的随机数 89
100-89
0-11的随机数 5
11-5
0-6的随机数
。。。
统统放到int数组就可以了
加载更多回复(6)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧